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/'
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))