[Django]-Reverse Not Found: Sending Request Context in from templates

9👍

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! 🙂

Leave a comment