Question

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!

Was it helpful?

Solution

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! :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top