On Wed, Oct 17, 2012 at 4:45 AM, lovetoprogram <stanbell2...@gmail.com> wrote:
> Hi,
>
> I am new to Django and I was creating a basic site with it. But I have
> encountered a problem.
>
> In the following code, I have a webpage with form and when submit button is
> clicked, the parameters have to be passed into a results page for display.
> When I keep render_to_response of the results page, the results page is
> shown instead of the main page.
>
> How to solve this problem?
>
> def mix(request):
>     if request.method == 'GET':
>
>         valueList = []
>         valueListInt = []
>         for i in range(1,20):
>             questionNum = ('qn'+str(i))
>             valueList.append(request.GET.getlist(questionNum))
>         #return redirect('mixResult.html', {'qn1Ans':str(valueList[0]),
> 'qn2Ans':valueList[1], 'qn3Ans':valueList[2], 'qn4Ans':valueList[3],
> 'qn5Ans':valueList[4], 'qn6Ans':valueList[5], 'qn7Ans':valueList[6],
> 'qn8Ans':valueList[7], 'qn9Ans':valueList[8], 'qn10Ans':valueList[9],
> 'qn11Ans':valueList[10], 'qn12Ans':valueList[11], 'qn13Ans':valueList[12],
> 'qn14Ans':valueList[13], 'qn15Ans':valueList[14], 'qn16Ans':valueList[15],
> 'qn17Ans':valueList[16], 'qn18Ans':valueList[17], 'qn19Ans':valueList[18],})
>         total = 0
>         for i in range(len(valueList)):
>             total = total + i
>         print 'total', total
>         if total == 171:
>             print 'wooooooooooooooooohooooooooooooooo'
>
>         return render_to_response('mix.html')

"redirect" is a message to the user's browser, telling it to GET a different
page.  It specifies a URL only.*  So the Django helper function only accepts
a URL.  It doesn't render a template.  It doesn't take a context dictionary.

(* A URL *CAN* include query parameters when needed, but they must be
encoded, and the URL is still just a string.)

If you used render_to_response with the arguments you are showing for
your commented out render call, it would display the results (assuming the
remainder of the code is correct, I'm not sure that it is).

The usual approach to having a form page and a results page requires two
views.  It is possible to do both with one view, by testing whether the form
has been submitted or not.  I suspect that you are trying to do that by when
you compare request.method to 'GET'.  The trouble with that is that that
the initial request for the page, without the form having been submitted, is
also a 'GET'.  The form submission can be something else, if you specify
it in the method attribute of the form, but it defaults to 'GET'.  (Often we
use 'POST' as the form method, but then you would have to learn to deal
with the CSRF mechanisms to get it to work, and that's an aside from the
current learning steps.  So you will want to replace:

    if request.method == 'GET':

with something like:

    if 'qn1' in request.GET:

Bill

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to