0

我有一个带有 POST Submit 按钮的表单。我想在提交表单和 form.is_valid() 时链接到新页面。

在视图中或模板中创建链接更好吗?怎么做?

视图.py:

from django.shortcuts import render_to_response
from ezmapping.models import *
from django.forms.models import modelformset_factory

def setName(request):
    ezAppFormSet = modelformset_factory(ezApp, extra=1, fields=('name'))
    formset = ezAppFormSet(queryset=ezApp.objects.none())
    if request.method == 'POST':
        formset = ezAppFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
    return render_to_response("project/manage_new.html", {'formset': formset, 'title': "New"}, context_instance=RequestContext(request))

模板.html

 {% extends "basemap.html" %}
 {% block content %}
 <table border="1">
   <tr>
   <td>
    <h1>Define new App options</h1>
    {% if formset.errors %}
        <p style="color: red;">
            Please correct the error{{ formset.errors|pluralize }} below.
        </p>
    {% endif %}
    <form method="post" action="." encrypt="multipart/form-data">{% csrf_token %}
            {{ formset.as_p }}
        <input type="submit" value="Submit">
    </form>
</td>
</tr>
</table>
{% endblock %}
4

3 回答 3

1

您可以像这样在视图中使用 HttpResponseRedirect():

from django.shortcuts import render_to_response
from ezmapping.models import *
from django.forms.models import modelformset_factory

def setName(request):
    ezAppFormSet = modelformset_factory(ezApp, extra=1, fields=('name'))
    formset = ezAppFormSet(queryset=ezApp.objects.none())
    if request.method == 'POST':
        formset = ezAppFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            next_link = u"/next/"
            return HttpResponseRedirect(next_link)

    return render_to_response("project/manage_new.html", {'formset': formset, 'title': "New"}, context_instance=RequestContext(request))
于 2013-02-18T16:50:54.703 回答
0

当然。只需在您的条件 for 中返回一个 render_to_response request.method == 'POST',或者可选地,只需将上下文和模板设置为变量以传递给 render_to_response ,如下所示:

释义:

def foo(request):
    template = 'template1.html'
    # form and formsets here set into `context` as a dictionary

    if request.method == 'POST':
        template = 'template2.html'

    return render_to_response(template, context)

[编辑]

当我重新阅读您的问题时,如果您想在表单有效的情况下重定向到不同的视图,请改为返回 HttpResponseRedirect 。

于 2013-02-18T16:45:19.053 回答
0

如果我正确理解你的问题。

您的看法:

from django.shortcuts import render_to_response
from ezmapping.models import *
from django.forms.models import modelformset_factory

def setName(request):
    ezAppFormSet = modelformset_factory(ezApp, extra=1, fields=('name'))
    formset = ezAppFormSet(queryset=ezApp.objects.none())
    if request.method == 'POST':
        formset = ezAppFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            submit_link = True # 2*
        else:
            submit_link = False
    return render_to_response("project/manage_new.html", {'submit_link': submit_link, 'formset': formset, 'title': "New"}, context_instance=RequestContext(request))

您的模板:

{% extends "basemap.html" %}
 {% block content %}
 <table border="1">
   <tr>
   <td>
    <h1>Define new App options</h1>
    {% if formset.errors %}
        <p style="color: red;">
            Please correct the error{{ formset.errors|pluralize }} below.
        </p>
    {% endif %}
    <form method="post" action="." encrypt="multipart/form-data">{% csrf_token %}
            {{ formset.as_p }}
        <input type="submit" value="Submit">
    </form>

    {% if submit_link %} 
      <a href='/next/'>Data is saved, let's continue.</a>
    {% endif %}
</td>
</tr>
</table>
{% endblock %}

更新

是的,如果您想重定向(不显示下一个链接),只需放置:

from django.shortcuts import redirect
...
return redirect( 'you-url' )

而不是 2*。

于 2013-02-18T16:47:44.010 回答