4

我有一个接受用户输入的视图,并且在成功发布后,它会重定向到另一个页面。教程中的代码几乎相同:

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)

网址.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'),
)

该代码似乎可以正常工作并且页面行为正确。

我的问题是客户端中出现的 URL 没有正确更新。原始页面是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将 url 显示为http://foo.com/quex/<id>/1/#/quex/<id>/2/.

4

5 回答 5

1

没有测试,但是:

网址.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'),
)

视图.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)
于 2014-01-21T17:32:30.717 回答
0

您的最后一个 quex 正则表达式以 / 结尾,而不是 /$。先改变那个。您说您已在视图中切换到反向,以便单个正则表达式更改应该起作用。

于 2014-01-18T18:19:25.853 回答
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. render_to_response()缺少尾随)
  2. 第三个参数render_to_response()是可选的。我通常把它关掉。但是,我会根据文档将其作为命名参数传递。在此处输入链接描述
  3. 最后:你def quex(request, id, question_number):应该是def quex(request, identifier, question_number):为了匹配 url 模式。还将 quex 正文中的 id 引用更改为标识符(我包含在示例代码中)
于 2014-01-24T19:52:12.127 回答
0

您收到此错误是因为您将视图方法作为递归调用。这里 return HttpResponseRedirect('/quex/' + id + '/' + str(next_question)) 再次调用相同的方法,唯一的区别是 request.method 将从 POST 更改为 GET。第二次作为 get 方法,这将调用 return render_to_response ,您将看到 page. 我希望你能解决我的问题。

像这样更改您的代码:

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)

此代码将用作您当前的代码。没有任何错误。希望这对您有所帮助。

于 2014-01-21T21:53:40.607 回答
0

浏览器在 URL 栏中显示的内容取决于您的客户端代码:

  • 简单的 HTML 表单将正确重定向。
  • jquery 的$.ajax()调用默认不会重定向,所以即使代码有效,客户端也不会重定向。
于 2014-01-23T06:59:56.640 回答