字段标签酥脆形式Django

mad*_*eer 6 django django-forms django-crispy-forms

我想为两种形式使用相同的模型并更改字段的标签如何更改标签?

这是我的一个表格:

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)
Run Code Online (Sandbox Code Playgroud)

我希望将"位置"更改为"工作地点"..我该怎么做?

ari*_*rie 14

这个问题并不是特定于Django Crispy Forms.

一种选择是在init()你的方法中设置标签JobPostForm.

def __init__(self, *args, **kwargs):
    super(JobPostForm, self).__init__(*args, **kwargs)
    self.fields['location'].label = "Job Location"
Run Code Online (Sandbox Code Playgroud)

处理这类问题时的一个很好的解读是重载Django表单字段.

  • 对于未来的读者:我有同样的问题,我解决了它确保`self.fields ['location'].label ="Job Location"`设置******(JobPostForm,self).__ init __(*args) ,**kwargs)`.@ arie的回答对我有用. (2认同)