form-horizo​​ntal 不适用于 django-crispy-forms 和 django-registration-redux

Wal*_*ite 4 django django-forms django-registration django-crispy-forms twitter-bootstrap-3

免责声明:我对 Django 和 python 完全是新手。

我正在使用带有 django 注册 redux 的脆皮表单。我已将 form-horizo​​ntal 应用于注册表单的子类,但它不会在与输入字段相同的行上呈现标签,如下所示: https: //i.stack.imgur.com/geYun.jpg

我已将 form-horizo​​ntal 硬编码到模板中的 html 中,将表单内容移动到容器的外部: https: //i.stack.imgur.com/vUXKQ.jpg

我尝试创建一个名为 SupplyhelixRegistrationForm 的子类表单,其中包含标签和字段类,脆皮表单文档说包括:(将包含链接,但没有足够的声誉)

来自注册 redux 应用程序的 forms.py 的一部分,其中包括我的修改:

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field
from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions

User = UserModel()


class RegistrationForm(UserCreationForm):


    required_css_class = 'required'
    email = forms.EmailField(label=_("E-mail"))

    class Meta:
        model = User
        fields = (UsernameField(), "email")



class supplyhelixRegistrationForm(RegistrationForm):

    def __init__(self, *args, **kwargs):
        super(supplyhelixRegistrationForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()  
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-8'
Run Code Online (Sandbox Code Playgroud)

然后我修改了注册reduxviews.py以指向supplyhelixRegistrationForm:

REGISTRATION_FORM_PATH = getattr(settings, 'REGISTRATION_FORM', 'registration.forms.supplyhelixRegistrationForm')
REGISTRATION_FORM = import_string( REGISTRATION_FORM_PATH )
Run Code Online (Sandbox Code Playgroud)

这是模板:

{% extends "registration/registration_base.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% crispy RegistrationForm RegistrationForm.helper %}

{% block title %}{% trans "Register for an account" %}{% endblock %}

{% block content %}


<div class="container  vertical-center">
  <div class="col-sm-6 col-sm-offset-3">
    <div class="panel  formBackground">

      <div class="panel-heading text-center">
        <h2>Transform your supply chain today!</h2>
      </div>
      <div class="panel-body">


        <form  method="post" action="">
           {% csrf_token %}
           {{ form|crispy}}
           <input class="btn btn-lg btn-primary btn-block" type="submit"  value="{% trans 'Register Now' %}" />
        </form>
      </div>
    </div>
  </div>
</div>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

我感谢您能提供的任何帮助。提前致谢!

YPC*_*ble 5

问题是您正在使用清晰的模板渲染表单,但由于您将其用作模板变量 ( {{ some_variable }}) 而不是使用{% crispy form_name %}标签,因此FormHelper属性未正确应用。

尝试以下更改:

  1. {{ form | crispy }}用。。。来代替{% crispy form %}
  2. FormHelper如果您想继续手动渲染标签,请添加以下属性<form></form>

self.helper.form_tag = False

  1. 您可能需要删除当前尝试手动创建效果的两行.form-horizontal

    self.helper.label_class = 'col-lg-2'

    self.helper.field_class = 'col-lg-8'

...DCF 会自动将 Bootstrap .form-horizontalCSS 类添加到您的表单中,这些类可能会干扰这些自动添加。