Django ModelForm to have a hidden input

前端 未结 4 809
孤城傲影
孤城傲影 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:41

    I posted a way to do it with generic class-based views here:

    from django.forms import HiddenInput
    
    from django.forms.models import modelform_factory
    
    _patient_create_form = modelform_factory(
        models.Patient,
        fields=['name', 'caregiver_name', 'sex', 'birth_date',
                'residence', 'country'],
        widgets={'country': HiddenInput()})
    
    class PatientCreate(LoginRequiredMixin, UserOrgRequiredMixin, CreateView):
        form_class = _patient_create_form
        template_name = 'healthdbapp/patient_form.html'
    

提交回复
热议问题