我想对两个表单使用相同的模型并更改字段标签,如何更改标签?
这是我的一种形式:
class jobpostForm(forms.ModelForm):
class Meta:
model = jobpost
fields = ('job_type','title','company_name','location','country','description','start_date','end_date','how_to_apply')
widgets = {
'job_type':RadioSelect(),
'location':TextInput(attrs={'size':'70','cols': 10, 'rows': 20}),
'description': TinyMCE(attrs={'cols':'100', 'row': '80'}),
'start_date':AdminDateWidget(attrs={'readonly':'readonly'}),
'end_date':AdminDateWidget(attrs={'readonly':'readonly'}),
'how_to_apply':RadioSelect(),
}
def __init__(self, *args, **kwargs):
super(jobpostForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'horizontal-form'
self.helper.form_id = 'id-jobpostform'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.form_action = '/portal/next/post/'
self.helper.add_input(Submit(_('submit_addcontent'), 'Preview'))
super(jobpostForm, self).__init__(*args, **kwargs)
就像我想将'位置'更改为'工作地点'..我该怎么做?
答案 0 :(得分:13)
这个问题并不是Django Crispy Forms特有的。
一种选择是在JobPostForm
的init()
方法中设置标签。
def __init__(self, *args, **kwargs):
super(JobPostForm, self).__init__(*args, **kwargs)
self.fields['location'].label = "Job Location"
在处理这类问题时,请仔细阅读Overloading Django Form Fields。
答案 1 :(得分:0)
有一种更简便的方法来执行此操作。参见下面的示例:
class CreateAPIKey(forms.ModelForm):
class Meta:
model = APIKey
fields = ["client_id"]
labels = {
"client_id": "Nome da Key",
}
help_texts = {
"client_id": _(
"Um identificador exclusivo de formato livre da chave. 50 caracteres no máximo"
),
}
widgets = {
"client_id": forms.TextInput(
attrs={
"id": "key_id",
"required": True,
"placeholder": "Entre um nome único para a chave",
"label": "dasdasdsdasd",
}
),
}
然后,然后在模板上呈现表单:
<form class="" method="POST" action="#">
{% csrf_token %}
<div class="col-md-4 col-sm-12 form-group" data-children-count="1">
{% for field in form %}
{{ field|as_crispy_field }}
{% endfor %}
</div>
</form>
结果: