繁体   English   中英

表格标签未显示在 html 上,表格较脆

[英]form labels not showing on html with a crispy form

我找不到使表单标签显示在模板中的方法。 我一直在 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>

您必须使用{% load crispy_forms_tags %}来初始化清晰的表单标签 - 例如在模板的顶部。 脆皮也“取代”了<form>...</form>

{% extends 'base.html' %}
{% load crispy_forms_tags %} <!-- HERE -->

{% block title %}
    Title
{% endblock title %}

{% block content %}
        {% csrf_token %}
        {% crispy form %}
{% endblock %}

像这样的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM