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.
- [Django]-Prefetch_related() join all on first item
- [Django]-Resize image in the views and load it in the templates in django
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.
- [Django]-Unknown command: 'collectstatic' Django 1.7
- [Django]-.js files not included in page when using Django Form Assets (Media class) with crispy forms
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})
- your
render_to_response()
is missing the trailing)
- 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 - finally: your
def quex(request, id, question_number):
should bedef 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)
- [Django]-.js files not included in page when using Django Form Assets (Media class) with crispy forms
- [Django]-Unknown command: 'collectstatic' Django 1.7