Django ModelForm to have a hidden input

前端 未结 4 818
孤城傲影
孤城傲影 2020-12-01 02:03

So I have my TagStatus model. I\'m trying to make a ModelForm for it. However, my form requires that the hidden input be populated with the {{ tag.name }}. I\'ve been lookin

4条回答
  •  甜味超标
    2020-12-01 02:23

    To make a field in a ModelField a hidden field, use a HiddenInput widget. The ModelForm uses a sensible default widget for all the fields, you just need to override it when the object is constructed.

    class TagStatusForm(forms.ModelForm):
        class Meta:
            model = TagStatus
            widgets = {'tag': forms.HiddenInput()}
    

提交回复
热议问题