在一页上显示多个表单的HTML代码

时间:2018-12-01 12:45:48

标签: django django-forms django-templates django-views

下面是视图

def TestPageView(request):
    if request.method == 'POST':
        contactform = ContactForm(request.POST,prefix="contact")
        subscriptionform = SubscriptionForm(request.POST,prefix="subscription")
        suggestionform  = SuggestionForm(request.POST,prefix="suggestion")
        globalmessageform = GlobalMessageForm(request.POST,prefix="globalmessage")

        if contactform.is_valid() and subscriptionform.is_valid() and suggestionform.is_valid() and globalmessageform.is_valid():
            contact = contactform.save()
            subscription = subscriptionform.save()
            suggestion = suggestionform.save()
            globalmessage = globalmessageform.save()

        else:
            print(form.errors)
    else:
        contactform = ContactForm(prefix="contact")
        subscriptionform = SubscriptionForm(prefix="subscription")
        suggestionform  = SuggestionForm(prefix="suggestion")
        globalmessageform = GlobalMessageForm(prefix="globalmessage")

    return render(request,'dashboard/test_page.html',{'contactform':contactform,'subscriptionform':subscriptionform,'suggestionform':suggestionform,'globalmessageform':globalmessageform})

如何编写html代码以在test_page.html上显示和保存这些表单。我知道如何显示一种表单,但是在这种情况下有4种表单。

我已经这样编码,但是在test_page.html上看不到任何输出。页面完全空白。

{% extends "base.html" %}
{% block content %}
{% load static %}  
<div>
<div>
 <form method="post" > 
    {% csrf_token %}
    {{ contactform.as_p }}
 </form>
 </div>

 <div>
 <form method="post" > 
    {% csrf_token %}
    {{ subscriptionform.as_p }}
 </form>
 </div>

<div>
<form method="post" > 
    {% csrf_token %}
    {{ suggestionform.as_p }}
</form>
</div>

<div>
<form method="post" > 
    {% csrf_token %}
    {{ globalmessageform.as_p }}
</form>
</div>

<input type="submit" name="Save">
</div>
{% endblock %}

0 个答案:

没有答案