如何在同一个模板上使用两个不同的Django表单?

Ami*_*Pal 5 django django-templates django-models django-forms django-views

我的forms.py:

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

AlertCountFormset = modelformset_factory(Alert,
                                       form = AlertForm)
Run Code Online (Sandbox Code Playgroud)

另一个Django Form类:

class NotifierForm(forms.ModelForm):
high = forms.ChoiceField(choices=NOTIFIER_TYPE)
medium = forms.ChoiceField(choices=NOTIFIER_TYPE)
low = forms.ChoiceField(choices=NOTIFIER_TYPE)  

def save(self, commit=True):
    alert = super(NotifierForm, self).save(commit=False)
    alert.high = self.cleaned_data["high"]
    alert.medium = self.cleaned_data["medium"]
    alert.low = self.cleaned_data["low"]
    alert.save()
    return alert

class Meta:
    model=Notifier
    fields = ('high','medium', 'low', 'user')
    widgets = {
        'user': forms.HiddenInput()
    }

NotifierFormset = modelformset_factory(Notifier,
                                    form = NotifierForm)
Run Code Online (Sandbox Code Playgroud)

以下是选择字段:

NOTIFIER_TYPE = (
(0, _('E-mail')),
(1, _('Skype')),
(2, _('IRC'))
)
Run Code Online (Sandbox Code Playgroud)

我想在同一个模板中填写这两个表单.所以我选择为两者写相同的视图:

def profile_setting(request, slug):
if request.method == 'POST':
    alert_form = AlertForm(request.POST)
    notifier_form = NotifierForm(request.POST)
    if alert_form.is_valid() and notifier_form.is_valid():
        alert = alert_form.save(commit=False)
        notifier = notifier_form.save(commit=False) 
        alert.user = request.user.username
        notifier.user = request.user.username
        notifier.save()
        alert.save()
        return HttpResponseRedirect(reverse('profile_setting', args=[slug]))

extra_context = {
    'alert_form': AlertForm(),
    'notifier_form': NotifierForm()
}
return direct_to_template(request,'users/user_profile_setting.html',
                          extra_context)
Run Code Online (Sandbox Code Playgroud)

根据我的template.html中的那个:

{% block content %}
<h3>{% trans "Alerts limit" %}</h3>
<form action="" method="POST">{% csrf_token %}
    {{ alert_form.as_p }}
    <input type="submit" value="{% trans 'Update' %}" />
</form>

<h3>{% trans "Notifier setting" %}</h3>
<form action="" method="POST">{% csrf_token %}
    {{ notifier_form.as_p }}
    <input type="submit" value="{% trans 'Update' %}" />
</form>
Run Code Online (Sandbox Code Playgroud)

一切都是正确的,它也将数据保存到数据库中.但问题是每当我填充aler_form并点击更新buttone时.它还使用相同的值更新另一个表单,反之亦然.例如,如果我选择

1 2 3 for high , medium and low for alert_Form
Run Code Online (Sandbox Code Playgroud)

然后它还为no​​tify_form保存相同的值.为什么会这样呢?这些观点有问题吗?

Ala*_*air 12

使用prefix参数,以便您的字段名称不会发生冲突.

例如:

alert form = AlertForm(request.POST, prefix='alert') 
notifier_form = NotifierForm(request.POST, prefix='notifier')
Run Code Online (Sandbox Code Playgroud)

您需要在未绑定的表单中使用相同的前缀.

extra_context = { 'alert_form': AlertForm(prefix='alert'),  notifier_form': NotifierForm(prefix='notifier') }
Run Code Online (Sandbox Code Playgroud)

使用前缀的优点是您不需要手动重命名字段,正如umnik700在答案中所建议的那样.