Django重定向传递变量

时间:2011-05-18 07:48:00

标签: django django-views

我有一个POST提交,我需要在重新加载页面时传递变量'count'。我目前正在使用render_to_response,但我认为这是错误的方法(尽管它目前正在运行)。这是我的代码 -

def email(request):
    count=0
    emails = EmailList.objects.order_by('network')
    networks = Network.objects.all()
    form = EmailListForm(request.POST)
    if request.method == 'POST' and form.is_valid():
        if form.cleaned_data.get('domain'):
                EmailList.objects.create(domain=form.cleaned_data['domain'], network=Network.objects.get(network=request.POST['domain_network']))
                return redirect('.') # should be using a redirect() ???
        if form.cleaned_data.get('email'):
            for x in form.cleaned_data.get('email'):
                EmailList.objects.create(email=x, network=Network.objects.get(network=request.POST['email_network']))
                count += 1
           # this next line is the one I'm asking about
            return render_to_response('email_add.html',{'networks':networks, 'emails':emails, 'form':form, 'count':count}, context_instance = RequestContext(request))
return render_to_response('email_add.html',{'networks':networks, 'emails':emails, 'form':form}, context_instance = RequestContext(request))

我是否需要更换render_to_response?如果是这样,那新代码需要什么?

2 个答案:

答案 0 :(得分:1)

如何保存需要传递给会话中其他视图的变量。 See Django's session documentation了解更多信息。

答案 1 :(得分:0)

无论如何,您只需传递count即可。您不需要单独的render_to_response。在做模板之前测试它在模板中是非零的。