我有一个视图接受来自用户的输入,并且在成功的帖子上,它会重定向到另一个页面。它与教程中的代码完全相同:
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'),
)
代码似乎有效,页面行为正常。
我的问题是客户端中显示的网址无法正确更新。原始页面为http://foo.com/questionnaire/ / 1 /,重定向页面为http://foo.com/questionnaire/ / 2 /。即使在重定向之后,旧URL仍继续显示在浏览器的地址栏中。
服务器状态消息看起来很好:
[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
我做错了什么?如何让浏览器显示正确的URL?
编辑:我做了一些测试。 Chrome,Safari(在OS X和iOS上)和Firefox如上所述显示URL。但iOS上的Browser Khmer会将网址显示为http://foo.com/quex/<id>/1/#/quex/<id>/2/
。
答案 0 :(得分:1)
没有测试,但是:
<强> 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 :(得分:0)
你的最后一个quex正则表达式以/结尾,而不是/ $。先改变一下。你说你已经在视图中切换到反转,这样单个正则表达式更改就可以了。
答案 2 :(得分:0)
由于您正在将views方法作为递归调用,因此出现此错误。这里返回HttpResponseRedirect('/ quex /'+ id +'/'+ str(next_question))再次调用同样的方法,唯一的区别是request.method将从POST更改为GET。第二次作为get方法,这将调用return render_to_response,你会看到页面。我希望你能完成我的工作。
更改您的代码:
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)
此代码将作为您当前的代码。没有任何错误。 希望这对你有所帮助。
答案 3 :(得分:0)
浏览器在网址栏中显示的内容取决于您的客户端代码:
$.ajax()
来电not redirect by default,所以即使代码有效,客户也不会重定向。答案 4 :(得分: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})
render_to_response()
缺少尾随)
render_to_response()
的第三个参数是可选的。我通常会把它关掉。但是,根据文档,我会将其作为命名参数传递。 enter link description here def quex(request, id, question_number):
应为def quex(request, identifier, question_number):
,以便与网址格式相匹配。同时将quex主体中的id引用更改为标识符(我在示例代码中包含)