[Django]-Django URLs – can't reverse url in a template

39👍

The syntax changed after Django 1.5
Instead of doing this:

<a href="{% url products_index %}"> Products </a>

You should now do this(string instead):

<a href="{% url 'products_index' %}"> Products </a>

9👍

You might try this instead:

urlpatterns = patterns('products.views',
    url(r'^$', view="index", name="products_index"),
)

/templates/products/index.html

<a href="{% url products_index %}"> Products </a>

Unless there’s a compelling reason you want to namespace your urls, it’s way easier just to use a more precise name in urls.py and then use that name in the url template tag.

Update

If the error you’re getting is No module named urls then that means one of the urls.py files isn’t being read in by the django project. Did you make sure that products has been added to INSTALLED_APPS in the settings.py file? Also, please include a stacktrace in your question so it will be easier to identify where the error is taking place.

Leave a comment