当我尝试访问http://127.0.0.7:8000/edit-paragraph/6/edit/时,我收到一个页面未找到,并出现以下错误:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^login/$
^login/home/
^logout/$
^edit-section/(?P<s_id>\d+)/edit/
^edit-section/(?P<s_id>\d+)
^edit-paragraph/(?P<p_id>\d+)/edit/
The current URL, edit-paragraph/6/, didn't match any of these.
在我的 urls.py 中,我有:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^login/$', 'mysite.auth.views.login_user'),
(r'^login/home/', 'mysite.auth.views.logged_in'),
(r'^logout/$', 'mysite.auth.views.logout_user'),
(r'^edit-section/(?P<s_id>\d+)/edit/', 'mysite.auth.views.edit_section_form'),
(r'^edit-section/(?P<s_id>\d+)', 'mysite.auth.views.edit_section'),
(r'^edit-paragraph/(?P<p_id>\d+)/edit/', 'mysite.auth.views.edit_paragraph')
)
对我来说,显然 URL http://127.0.0.7:8000/edit-paragraph/6/edit/应该与我的 URLConf 的最后一行匹配。关于我做错了什么的任何想法?它能够匹配类似的网址:
r'^edit-section/(?P<s_id>\d+)/edit/'
提前致谢!
编辑:
事实证明它正在重定向。现在我有了这个,浏览器给出了一个找不到页面的错误:
@login_required
def edit_paragraph(request, p_id):
p = get_object_or_404(Paragraph, id=p_id)
if request.method == 'POST':
form = ParagraphForm(request.POST)
if form.is_valid():
p.title = request.POST['title']
p.description = request.POST['description']
p.save()
return HttpResponse('foo')
else:
form = ParagraphForm({ 'title': p.title, 'description': p.description })
return render_to_response('auth/edit-paragraph.html', { 'form': form }, context_instance=RequestContext(request))
return HttpResponseRedirect('/edit-paragraph/'+p_id+'/edit/')
编辑 - 已解决:
这是我为避免无限循环以及发生的任何其他问题而提出的解决方法:
@login_required
def edit_paragraph(request, p_id):
p = get_object_or_404(Paragraph, id=p_id)
form = ParagraphForm({ 'title': p.title, 'description': p.description })
if request.method == 'POST':
form = ParagraphForm(request.POST)
if form.is_valid():
p.title = request.POST['title']
p.description = request.POST['description']
p.save()
return HttpResponseRedirect('/login/home')
return render_to_response('auth/edit-paragraph.html', { 'form': form }, context_instance=RequestContext(request))