Using HttpResponseRedirect, but the browser does not show the correct URL

I have a view that accepts input from the user and in a successful message, it redirects to another page. This is almost the same code in the tutorial:

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 # ... return HttpResponseRedirect('/quex/' + id + '/' + str(next_question)) else: form = QuestionForm() # An unbound form return render_to_response('questionnaire.html', { 'form': form, 'id' : id, 'question_number' : question_number}, RequestContext(request) 

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

The code works and the page behaves correctly.

My problem is that the URL displayed on the client is not updating correctly. The original page is http://foo.com/questionnaire/ / 1 /, and the redirected page is http://foo.com/questionnaire/ / 2 /. The old URL continues to appear in the address bar of the browser even after redirecting.

Server status messages look great:

 [19/Aug/2013 19:15:40] "GET /quex/P54C9UCS/1/ HTTP/1.1" 200 3225 [19/Aug/2013 19:15:44] "POST /quex/P54C9UCS/1/ HTTP/1.1" 302 0 [19/Aug/2013 19:15:44] "GET /quex/P54C9UCS/2/ HTTP/1.1" 200 3206 

What am I doing wrong? How can I get the browser to display the correct URL?

Edit: I did a bit more testing. Chrome, Safari (on OS X and iOS) and Firefox display the URL as described above. But the Khmer Browser on iOS shows the URL as http://foo.com/quex/<id>/1/#/quex/<id>/2/ .

+4
source share
5 answers

Not tested, 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) 
+1
source

Your last quex key expression ends with /, not / $. Change this first. You say that you switched to the opposite in the view, so one change in the regex should work.

0
source

You get this error because you are calling the views method as recursion. here return HttpResponseRedirect ('/ quex /' + id + '/' + str (next_question)) calls the same method again, only the difference - request.method will change from POST to GET. and the second time, as the get method, it will call return render_to_response, and you will see the page. I want you to go through.

Change your code as follows:

 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 like your current code. no mistakes. Hope this helps you.

0
source

What the browser displays in the URL bar will depend on your client code:

  • simple HTML forms will be redirected correctly.
  • jQuery $.ajax() calls are not redirected by default , therefore, although the code works, the client does not redirect.
0
source
 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}) 
  • your render_to_response() missing trailing )
  • The third argument to render_to_response() is optional. I usually leave it. However, I would pass it as a named argument as per the documentation. enter the link here
  • finally : your def quex(request, id, question_number): must be def quex(request, identifier, question_number): to match the url pattern. Also change the id links in the body of quex as an identifier (which I included in the sample code)
0
source

Source: https://habr.com/ru/post/1497903/


All Articles