user758733
user758733

Reputation: 13

Django redirect with passing variables

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 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))

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

Upvotes: 1

Views: 1754

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239290

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.

Upvotes: 0

Torsten Engelbrecht
Torsten Engelbrecht

Reputation: 13486

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

Upvotes: 1

Related Questions