Home > Software engineering >  NoReverseMatch for Django and DjangoCMS App
NoReverseMatch for Django and DjangoCMS App

Time:12-19

I am trying to set up some URLs for tags and categories on a forum. The links look like this

<a href="{% url 'article-list-by-tag' %}{{ tag }}"  >{{ tag }}</a>

The URL pattern article-list-by-tag comes from a DjangoCMS app called AldrynNewsblog. It can be found HERE and looks like this

url(r'^tag/(?P<tag>\w[-\w]*)/',
    TagArticleList.as_view(), name='article-list-by-tag'),

Note that the NewsBlog does not have app_name declared in its urls.py.

The app itself is part of a larger Django app called DjangoCMS. The url entry-point for the latter is declared in project's main urls.py as follows:

path('', include('cms.urls')),

The error that I am getting is

Reverse for 'article-list-by-tag' not found. 'article-list-by-tag' is not a valid view function or pattern name.

What should I do to resolve this? I have tried adding namespace to various URL entries as well as app_name to NewsBlog to no avail. The Django version being used is 2.1

CodePudding user response:

You need to pass the .slug of the tag as parameter of the URL, so:

<a href="{% url 'article-list-by-tag' tag=tag.slug %}"  >{{ tag }}</a>

If you import it with a namespace, you need to prefix the name of the view with that prefix, so 'some_namespace:article-list-by-tag'.

CodePudding user response:

In case it helps anyone, the following code worked for my use-case:

<a href="{% namespace_url 'article-list-by-tag' tag=tag.slug %}" >{{ tag }}</a>

Where the namespace_url is a template tag provided by DjangoCMS.

  • Related