HTML布局对象不起作用

时间:2017-12-07 11:23:10

标签: django django-forms django-crispy-forms

我是Django Crispy Form的新手,我试图在我的表单中添加一个HTML对象,但它不起作用。所有其他元素都被渲染,但不是HTML。

这是我的表格:

forms.py

from crispy_forms.layout import Layout, Fieldset, HTML

class MyUserRegistrationForm(forms.ModelForm):
    class Meta:
        model = MyUser
        fields = ['title', 'privacy_disclaimer_accepted']

    def __init__(self, *args, **kwargs):
        super(MyUserRegistrationForm, self).__init__(*args, **kwargs)

    helper = FormHelper()
    helper.form_method = 'post'
    helper.form_class = 'form-horizontal'
    helper.label_class = 'col-sm-2'
    helper.field_class = 'col-sm-6'
    helper.form_error_title = 'Form Errors'
    helper.error_text_inline = True
    helper.help_text_inline = True
    helper.html5_required = True
    helper.form_tag = False
    helper.layout = Layout(
        Fieldset('Information', 'title'),
        Fieldset('Privacy Statement',
                 HTML("""
                    <div id="iframe" class="mt-5">
                        <h6>Notice/Disclaimer:</h6>
                        <div class="privacy-policy">
                            {% if privacy_disclaimer %}
                                {{ privacy_disclaimer }}
                            {% else %}
                                {% include "registration/privacy_policy.html" %}
                            {% endif %}
                        </div>
                    </div>
                 """),
         'privacy_disclaimer_accepted', )
    )

我有一个基本的HTML文件,我有正常的html,header和body标签。 这是HTML注册页面:

register.html

{% extends "base.html" %}

{% block title %}Create an account{% endblock %}

{% block content %}
<div class="registration-page">
    <h3 class="text-center">Create an account</h3>
    <div class="registration-page__form">

            {% if form.errors %}
            {% for field in form %}
            {% for error in field.errors %}
            <div class="alert alert-error">
                <strong>{{ error|escape }}</strong>
            </div>
            {% endfor %}
            {% endfor %}
            {% for error in form.non_field_errors %}
            <div class="alert alert-error">
                <strong>{{ error|escape }}</strong>
            </div>
            {% endfor %}
            {% endif %}

            <form action="" method="post">
                {% csrf_token %}
                {% load crispy_forms_tags %}
                {{ form_myuser|crispy }}
                <br/>
                <div class="text-right">
                    <input type="submit" class="btn btn-primary btn--action"  value="Create the account">
                </div>
            </form>
    </div>
</div>
{% endblock %}

修改

views.py

class RegisterView(View):
    template_name = 'registration/register.html'

    def _get_context_data(self, **kwargs):
        context = {}
        privacy_disclaimer = ''
        context['privacy_disclaimer'] = privacy_disclaimer
        if kwargs:
            context.update(kwargs)
        return context

    def get(self, request, *args, **kwargs):
        extra_context = {
            'form_myuser': MyUserRegistrationForm(),
        }
        context = self._get_context_data(**extra_context)
        return render(request, self.template_name, context)

1 个答案:

答案 0 :(得分:0)

我修复了问题,问题是表单加载。 我正以错误的方式加载它。 而不是{{ form_myuser|crispy }}我必须使用此代码{% crispy form_tolauser %}