如何在同一个模板中使用两个不同的 Django 表单? [英] How to use two different Django Form at the same template?

查看:35
本文介绍了如何在同一个模板中使用两个不同的 Django 表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 forms.py:

My forms.py:

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

AlertCountFormset = modelformset_factory(Alert,
                                       form = AlertForm)

另一个 Django 表单类:

Another Django Form class:

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)

以下是选择字段:

NOTIFIER_TYPE = (
(0, _('E-mail')),
(1, _('Skype')),
(2, _('IRC'))
)

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

I want to fill these two forms at the same template. So i choose to write same views for both i.e. :

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)

根据我的template.html:

According to that in my 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>

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

Everything is right, It is saving the data to database also. But the problem is whenever i filled the aler_form and click on the update buttone. it also update the another form with the same value or vice versa. For example if i choose

1 2 3 for high , medium and low for alert_Form

然后它也为notify_form保存相同的值.为什么会发生这种情况.观点有问题吗?

Then it also save the same value for notify_form. Why is this happening. Is that something wrong with the views?

推荐答案

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

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

例如:

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') }

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

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

这篇关于如何在同一个模板中使用两个不同的 Django 表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆