开发者

Django redirect with passing variables

开发者 https://www.devze.com 2023-03-06 12:47 出处:网络
I have a POST submission, and I need to pass the variable \'count\' when the page is re-loaded. I am currently using a render_to_response, but I believe this is the wrong approach (though it is curren

I have a POST submission, and I need to pass the variable 'count' when the page is re-loaded. I am currently using a render_to_response, but I believe this is the wrong approach (though it is currently working). This is the code I have --

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 b开发者_开发问答e 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))

Do I need to replace the render_to_response? If so, what does that new line of code need to be?


How about saving the variable you need to pass to this other view in a session. See Django's session documentation for more information.


You can simply pass count, regardless. You don't need separate render_to_responses. Test that it's non-zero in your template before doing whatever its used for.

0

精彩评论

暂无评论...
验证码 换一张
取 消