URL pattern matching in django
I am getting a Page Not Found when I try to access http://127.0.0.7:8000/edit-paragraph/6/edit/, with the following error:
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.
In my urls.py, I have:
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')
)
To me it clearly looks like the url http://127.0.0.7:8000/edit-paragraph/6/edit/ should match the last line of my URLConf. Any ideas as to what I'm doing wrong? It is able to match the similar urls:
r'^edit-section/(?P<s_id>\d+)/edit/'
开发者_Go百科
Thanks in advance!
EDIT:
It turns out it was redirecting. Now I have this, and the browser is giving a Page not found error:
@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/')
EDIT - Solved:
Here's the fix I came up with to avoid infinite looping, and whatever other problems were occurring:
@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))
A couple of things:
If you're being redirected, make sure you're actually being redirected and it's not simply that your browser is being too helpful with it's URL autocomplete function.
Your web developer tools (firebug, web inspector, etc.) will log the redirect if it's happening.
Many browsers have odd behavior around the trailing/
. You can check this by running your dev server on a port that your browser hasn't been to before (i.e.,manage.py runserver 8001
), then trying again.If you hit an exception somewhere in or before your
urls.py
(or your view function), it's possible that the url pattern isn't actually being evaluated so django thinks you have a 404. In your case, this doesn't seem to be happening because django told you which URLs it was checking. However, you might try importing yoursettings.py
,urls.py
, andviews.py
files in a console, and try running your view function directly and make sure you're not getting an exception.
精彩评论