【问题标题】:HTML layout object doesn't workHTML 布局对象不起作用
【发布时间】:2017-12-07 11:23:10
【问题描述】:

我对 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、标题和正文标签。 这是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)

【问题讨论】:

  • 显示视图。你确定你同时拥有formform_myuser
  • 是的,我喜欢@DanielRoseman。我在我拥有的注册视图中使用_get_context_data 方法中的form_myuser 更新上下文。模板没有损坏,一切都被渲染了,但 HTML 布局对象没有。
  • 请显示该视图。
  • 它就在那里!再次检查帖子。 @丹尼尔罗斯曼
  • 这个HTML对象到底是什么?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2018-05-12
    相关资源
    最近更新 更多