Adding a link to another page in Django(3.1) (NoReverseMatch)

Viewed 187

I'm trying to add a link to another page on my index page, but when I use {% url 'blog:BlogAbout' %} I get:

Exception Type: NoReverseMatch
Exception Value:'blog' is not a registered namespace

index.html:

<a href="{% url 'blog:BlogAbout' %}">About Me</a>

blog.urls.py:

path('about/', views.about, name="BlogAbout"),

blog.views.py:

def about(request):
    return render(request, 'blog/about.html')

urls.py:

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

The question is almost familiar to this: How do I add link to another page [Django 3.0]? Reverse not found

I'm doing it like there but I still get an error. What is the right way to add the link?

1 Answers

The blog: prefix is if you specify an app_name = 'blog' in the urls.py. If you did not do that, then there is no "namespace", and therefore the url is simply:

<a href="{% url 'BlogAbout' %}">About Me</a>

If you want to use a namespace, you specify this in the urls.py with:

app_name = 'blog'

urlpatterns = [
    path('about/', views.about, name='BlogAbout'),
    # …,
]

or, you can specify this with the namespace=… parameter in the include(…) function [Django-doc]:

    path('', include('blog.urls', namespace='blog')),

then you specify this with:

<a href="{% url 'blog:BlogAbout' %}">About Me</a>
Related