如何在Django的一个/单个页面中创建多个表单?

时间:2019-04-15 06:27:43

标签: python html django django-forms

我想在页面中使用2种不同的形式。但是,当我尝试保存表单的值时,它们仅保存在form_fridge中。我认为这是存在的,因为我将一个csrf_token用于2种形式。你能帮我吗?

    <form method="POST" enctype="multipart/form-data" id = "formTv">
    {% csrf_token %}
    {{ form_tv.as_p }}
    <button type="submit" class="save btn btn-default">Save</button>
    </form>

    <form method="POST" enctype="multipart/form-data" id = "formFridge">
    {% csrf_token %}
    {{ form_fridge.as_p }}
    <button type="submit" class="save btn btn-default">Save</button>
    </form>

views.py

def add_new(request):
    form_fridge = FridgeForm(request.POST, request.FILES, use_required_attribute=False)
    form_tv = TvForm(request.POST, request.FILES, use_required_attribute=False)
    if form_fridge.is_valid() and request.method == 'POST':
        form_fridge.save()
        return redirect('new')
    elif form_tv.is_valid() and request.method == 'POST':
        form_tv.save()
        return redirect('new')
    return render(request, 'appliances/add_new.html', {'form_tv': form_tv,
                                                       'form_fridge': form_fridge})

3 个答案:

答案 0 :(得分:0)

问题是您有两个单独的HTML <form>元素和提交按钮。如果要同时提交和处理两个表单,则它们必须位于同一表单元素中。

<form method="POST" enctype="multipart/form-data" id = "formTv">
{% csrf_token %}
{{ form_tv.as_p }}

{{ form_fridge.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>

在实例化视图中的表单时,您可能还想使用prefix参数,以避免名称相似的字段发生冲突。

答案 1 :(得分:0)

您应该添加一个隐藏字段,以区分提交的表单。

<form method="POST" enctype="multipart/form-data" id = "formTv">
    {% csrf_token %}
    {{ form_tv.as_p }}
    <input type="hidden" id="form_type" name="form_type" value="form_tv">
    <button type="submit" class="save btn btn-default">Save</button>
    </form>

    <form method="POST" enctype="multipart/form-data" id = "formFridge">
    {% csrf_token %}
    {{ form_fridge.as_p }}
    <input type="hidden" id="form_type" name="form_type" value="form_fridge">
    <button type="submit" class="save btn btn-default">Save</button>
    </form>

因此,在提交其中一种表单时,您可以找到提交的表单。

#views.py
def add_new(request):
    if request.method == "POST":  #check if the request is POST call or not.
        form_type = request.POST.get("form_type")
        form = None
        # check which form to evaluate
        if form_type == "form_fridge":
            form = FridgeForm(request.POST, request.FILES, use_required_attribute=False)

        if form_type == "form_tv":
            form = TvForm(request.POST, request.FILES, use_required_attribute=False)

        # evaluate the form
        if form and form.is_valid():
            form.save()
            return redirect('new')

    return render(request, 'appliances/add_new.html', {'form_tv': form_tv,
                                                       'form_fridge': form_fridge})

答案 2 :(得分:-1)

假设您要提交2种表格:

class ContactForm(forms.Form):
    title = forms.CharField(max_length=150)
    message = forms.CharField(max_length=200, widget=forms.TextInput)


class SubscriptionForm(forms.Form):
    email = forms.EmailField()

Django FormView是用于以这种方式处理表单的主要类。 至少需要:

一个form_class属性,指向我们表单的类 要处理。

success_url属性,指示要重定向到的URL 成功的表格处理。

一个执行实际处理逻辑的form_valid方法。

所以在您看来:

class MultipleFormsDemoView(MultiFormsView):
    template_name = "pages/cbv_multiple_forms.html"
    form_classes = {'contact': ContactForm,
                    'subscription': SubscriptionForm,
                    }

    success_urls = {
        'contact': reverse_lazy('contact-form-redirect'),
        'subscription': reverse_lazy('submission-form-redirect'),
    }

    def contact_form_valid(self, form):
        'contact form processing goes in here'

    def subscription_form_valid(self, form):
        'subscription form processing goes in here'

在模板上:

<form method="post">{% csrf_token %}
    {{ forms.subscription }}
    <input type="submit" value="Subscribe">
</form>
<form method="post">{% csrf_token %}
    {{ forms.contact }}
    <input type="submit" value="Send">
</form>

You might also want to check inline forms