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

前端 未结 2 1817
暗喜
暗喜 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:39

    Use the prefix argument so that your field names don't clash.

    For example:

    alert form = AlertForm(request.POST, prefix='alert') 
    notifier_form = NotifierForm(request.POST, prefix='notifier')
    

    You need to use the same prefix in your unbound forms.

    extra_context = { 'alert_form': AlertForm(prefix='alert'),  notifier_form': NotifierForm(prefix='notifier') }
    

    The advantage of using prefix is that you don't need to manually rename the fields, as umnik700 suggests in their answer.

提交回复
热议问题