[Django]-Using HttpResponseRedirect, but browser is not showing correct URL

1👍

Didn’t test it, but:

urls.py

urlpatterns = patterns('',    
    url(r'^$', 'django.contrib.auth.views.login'),
    url(r'^logout$', 'screening.views.logout_view'),
    url(r'^home/$', 'screening.views.home'),
    url(r'^quex/new/$', 'screening.views.new_quex'),
    # others omitted
    url(r'^quex/(?P<identifier>\w{8})/(?P<question_number>\d+)/$', 'screening.views.quex', name='quex-view'),
)

views.py

from django.core.urlresolvers import reverse_lazy


def quex(request, identifier, question_number):

    next_question = int(question_number) + 1
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass

            response_url = reverse_lazy('quex-view', kwargs={'identifier':identifier, 'question_number':next_question})
            return HttpResponseRedirect(reponse_url) 
    else:
        form = QuestionForm() # An unbound form

    return render_to_response('questionnaire.html', {
        'form': form,
        'identifier' : identifier,
        'question_number' : question_number},
        RequestContext(request)

0👍

Your last quex regex ends in /, not /$. Change that first. You say you’ve switched to reverse in the view so that single regex change should work.

0👍

You are getting this error because of you are calling views method as a recursion. here return HttpResponseRedirect(‘/quex/’ + id + ‘/’ + str(next_question)) is call again same method only difference is request.method will change from POST to GET. and second time as a get method this will call return render_to_response and you will see page. I hape you will get my through.

Change your code like this:

def quex(request, id, question_number):
    next_question = int(question_number) + 1
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            # Next line I have written, so you will get same value of question_number as you get after returning from HttpResponseRedirect
            question_number =  next_question + 1 
    form = QuestionForm() # An unbound form

    return render_to_response('questionnaire.html', {
        'form': form,
        'id' : id,
        'question_number' : question_number},
        RequestContext(request)

This code will work as your current code. without any bug.
Hope this will help full to you.

0👍

What the browser shows in the URL bar will depend on your client-side code:

  • simple HTML forms will redirect correctly.
  • jquery’s $.ajax() call will not redirect by default, so even though the code works, the client does not redirect.
👤dnozay

0👍

def quex(request, identifier, question_number):

    next_question = int(question_number) + 1
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/quex/' + identifier + '/' + str(next_question)) 
    else:
        form = QuestionForm() # An unbound form

    return render_to_response('questionnaire.html', {
        'form': form,
        'id' : identifier,
        'question_number' : question_number})
  1. your render_to_response() is missing the trailing )
  2. the 3rd argument to render_to_response() is optional. I usually leave it off. However I would pass it in as a named argument as-per the documentation. enter link description here
  3. finally: your def quex(request, id, question_number): should be def quex(request, identifier, question_number): in order to match the url pattern. Also change the id references in the body of quex to be identifier ( which I included in the sample code)

Leave a comment