3

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))
katherine
  • 261
  • 4
  • 15
  • 5
    In your error it says `edit-paragrah/6/ didn't match` - there is no `edit` at the end of the URL. Wrong error? – Pit Aug 25 '11 at 18:30
  • @Pit - Nope... I tell it to go to /edit-paragraph/6/edit/, and it redirects to that. – katherine Aug 25 '11 at 19:07
  • That's weird! The error is completely correct, as you do not have a rule that would match `edit-paragraph/6/`. Could we see your code of the `edit_paragraph`-view? Maybe there is a redirect that shouldn't be there! – Pit Aug 25 '11 at 19:10
  • @Pit I posted it... getting closer I think. I had it redirecting to ../ so that was the previous issue. – katherine Aug 25 '11 at 19:23
  • 1
    Getting a page not found (= 404) will propably happen at `get_object_or_404`. Are you sure that the id you were using (probably 6) exists? – Pit Aug 25 '11 at 19:26
  • 2
    aren't you redirecting yourself in a loop? – andrew cooke Aug 25 '11 at 19:28

1 Answers1

0

A couple of things:

  1. 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.

  2. 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 your settings.py, urls.py, and views.py files in a console, and try running your view function directly and make sure you're not getting an exception.

Seth
  • 45,033
  • 10
  • 85
  • 120