How to use two different Django Form at the same template?

前端 未结 2 1766
暗喜
暗喜 2021-02-06 10:39

My forms.py:

class AlertForm(forms.ModelForm):
class Meta:
    model=Alert
    fields = (\'high\',\'medium\', \'user\')
    widgets = {
        \'user\':  forms.         


        
2条回答
  •  无人及你
    2021-02-06 11:38

    The problem is that on your forms the fields have the same names.

    request.POST is a dictionary-like object. So it only contains the name/value pairs from the request. If the field names are the same on both of the forms then when you call

    alert_form = AlertForm(request.POST)
    notifier_form = NotifierForm(request.POST)
    

    they are initialized with the same values. To resolve this make the names to be unique between your two forms. For example prefix all the field names on the AlertForm with "alert_".

提交回复
热议问题