【问题标题】:form labels not showing on html with a crispy form表单标签未显示在 html 上,表单很脆
【发布时间】:2020-07-09 23:22:30
【问题描述】:

我找不到使表单标签显示在模板中的方法。我一直在 stackoverflow 上寻找几个小时的方法,但到目前为止没有任何效果。标签没有显示出来。

也许我的代码在某处有错误,但我无法发现它。

forms.py

class Contact(forms.ModelForm):
    class Meta:
        model = contact
        fields = ['first_name','last_name','email','phone']
        #labels = {'first_name':'First name','last_name':'Last name', 'email':'email', 'phone':'phone number'}
        
        def __init__(self, *args, **kwargs):
            super(Contact, self).__init__(*args, **kwargs)
            self.helper = FormHelper()
            self.helper.form_show_labels = True
            self.fields['first_name'].label = "First name"
            self.fields['last_name'].label = "Last name"
            self.fields['email'].label = "email"
            self.fields['phone'].label = "phone number"

html模板:

<p> {%  trans 'Or fill out the form below' %}</p> 
<form method="POST" class="form-validate" id="contact">
    {% csrf_token %}
    <a>{% crispy form %}
         <input type="submit" class="learn_more2" value={%  trans 'Contact us' %}  style="background-color:#36393f; color:#DB6574; margin:auto; display:block">
    </a>
</form>

【问题讨论】:

    标签: python django django-crispy-forms


    【解决方案1】:

    您必须使用{% load crispy_forms_tags %} 来初始化清晰的表单标签,例如在模板的顶部。还脆“取代”了&lt;form&gt; ...&lt;/form&gt;

    {% extends 'base.html' %}
    {% load crispy_forms_tags %} <!-- HERE -->
    
    {% block title %}
        Title
    {% endblock title %}
    
    {% block content %}
            {% csrf_token %}
            {% crispy form %}
    {% endblock %}
    

    类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      相关资源
      最近更新 更多