【发布时间】: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