views:

586

answers:

1

N.B This question has been significantly edited before the first answer was given.

Hi,

I'm fairly new to django, so apologies if I'm missing something obvious.

I've got a urls.py file that looks like this:

urlpatterns = patterns(
    '',
    (r'^$', 'faros.lantern.views.home_page'),
    (r'^login/$', 'django.contrib.auth.views.login'),
    (r'^logout/$', 'django.contrib.auth.views.logout'),
    (r'^about/$', 'faros.lantern.views.about_page_index', {}, 'about_page_index'),
    (r'^about/(?P<page_id>([a-z0-9]+/)?)$', 'faros.lantern.views.about_page', {}, 'about_page'),
)

Views that looks like this:

def about_page_index(request):
    try:
        return render_to_response('lantern/about/index.html', context_instance=RequestContext(request))
    except TemplateDoesNotExist:
        raise Http404

def about_page(request, page_id):
    page_id = page_id.strip('/ ')

    try:
        return render_to_response('lantern/about/' + page_id + '.html', context_instance=RequestContext(request))
    except TemplateDoesNotExist:
        raise Http404

And a template that includes this:

<a href="{% url lantern.views.about_page_index %}">Contact</a>
<a href="{% url lantern.views.about_page page_id="contact" %}">Contact</a>

I'm getting this error message:

Caught an exception while rendering: Reverse for '<function about_page at 0x015EE730>' with arguments '()' and keyword arguments '{'page_id': u'contact'}' not found. The first reverse works fine (about_page_index), generating the correct URL without error messages.

I think this is because the request argument to the about_page view (request) is used, so I need to pass it in when I generate the URL in my template. Problem is, I don't know how to get to it, and searching around isn't getting me anywhere. Any ideas?

Thanks,

Dom

p.s. As an aside, does that method of handling static "about" type pages in an app look horrific or reasonable? I'm essentially taking URLs and assuming the path to the template is whatever comes after the about/ bit. This means I can make the static pages look like part of the app, so the user can jump into the about section and then right back to where they came from. Comments/Feedback on whether this is djangoic or stupid appreciated!

+6  A: 

If I guess correctly from the signature of your view function (def about_page(request, page_id = None):), you likely have another URL configuration that points to the same view but that does not take a page_id parameter. If so, the django reverse function will see only one of these, and it's probably seeing the one without the named page_id regex pattern. This is a pretty common gotcha with reverse! :-)

To get around this, assign a name to each of the url patterns (see Syntax of the urlpatterns variable). In the case of your example, you'd do:

(r'^about/(?P<page_id>([a-z]+/)?)$', 'faros.lantern.views.about_page', 
 {}, 'about_with_page_id')

and then in the template:

<a href="{% url about_with_page_id page_id="contact" %}">Contact</a>


Edit

Thanks for posting the updated urls.py. In the url template tag, using the unqualified pattern name should do the trick (note that I'm deleting the lantern.views part:

<a href="{% url about_page_index %}">Contact</a>
<a href="{% url about_page page_id="contact" %}">Contact</a>


Edit2

I'm sorry I didn't twig to this earlier. Your pattern is expressed in a way that django can't reverse, and this is what causes the mismatch. Instead of:

r'^about/(?P<page_id>([a-z]+/)?)$'

use:

r'^about/(?P<page_id>[a-z0-9]+)/$'

I created a dummy project on my system that matched yours, reproduced the error, and inserted this correction to success. If this doesn't solve your problem, I'm going to eat my hat! :-)

Jarret Hardie
I actually don't have two views - I was trying to get to the same view with one URL configuration. I tried separating them into two views with two entries in the urls.py, but that didn't fix it. The about_without_page_id bit worked fine, but the about_with_page_id causes the same error.
Dominic Rodger
Sorry if I wasn't clear... the issue does arise when you have either two URL entries to one view, or you're trying to overload a single URL entry to one view. Can you post more of your urls.py file?
Jarret Hardie
But don't worry... you're not alone. Reverse is the devil I tell you :-) We all get bitten by it!
Jarret Hardie
Thanks for your help - I've edited the question with more details from urls.py, and the updated views I've got.
Dominic Rodger
Hey Jarret - thanks again, I tried it without the lantern.views part, and sadly, I get the same error
Dominic Rodger
What happens if you try changing the url pattern name from 'about_page' to something like 'about_page_foo'? Does that help at all?
Jarret Hardie
Nope - no luck :(
Dominic Rodger
Got it... editing post again :-)
Jarret Hardie
+1 for sheer effort ;)
Paolo Bergantino
I almost thought about opening a bounty so I could give you more than 12 rep. Thank you SO much!
Dominic Rodger
My pleasure to help :-)
Jarret Hardie
Ah, the slash after the regex is what caused the error for me, too. Thanks for the good tip.
Reinout van Rees