Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 2.0 'name' is not a registered namespace

I've seen this error posted several times, but I can't find anyone using Django 2.0 and having the problem.

The problem cropped up when I tried to nest one app inside another. The nested app (called "users") is meant to allow users to login and out. After putting that segment in, I'm getting the following error:

Template error:
In template C:\Users\arbit\Documents\python\learning_log\learning_logs\templates\learning_logs\base.html, error at line 6
   'users' is not a registered namespace
   1 : <p>
   2 :     <a href="{% url 'learning_logs:index' %}">Learning Log</a> 
   3 :     <a href="{% url 'learning_logs:topics' %}">Topics</a> 
   4 :     {% if user.is_authenticated %}
   5 :         Hello, {{ user.username }}.
   6 :         <a href=" {% url 'users:logout' %} ">log out</a>
   7 :     {% else %}
   8 :         <a href="{% url 'users:login' %}">log in</a>
   9 :     {% endif %}
   10 : </p>
   11 : 
   12 : {% block content %}{% endblock content %}
   13 : 

Here's my root urls.py

from django.urls import path, include
from django.contrib import admin

from . import views

app_name = "learning_log"

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/', include('users.urls')),
    path('', include('learning_logs.urls')),
]

urlpatterns = [
    # Home page
    path('', views.index, name='index'),

    # Show all topics
    path('topics/', views.topics, name='topics'),

    # Detail page for a single topic
    path('topics/<int:topic_id>/', views.topic, name='topic'),

    # Page for adding a new topic
    path('new_topic/', views.new_topic, name='new_topic'),

    # Page for adding a new entry
    path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),

    # Page for editing an entry
    path('edit_entry/<int:entry_id>/', views.edit_entry, name='edit_entry'),
]

... and the app "users" url.py

from django.urls import path
from django.contrib.auth.views import login

from . import views

app_name = 'users'

urlpatterns = [
    # Login page.
    path('login/', login, {'template_name': 'users/login.html'}, name='login'),

    # Logout page
    path('logout/', views.logout_view, name='logout'),
]

and the base.html from the "users" app

<p>
    <a href="{% url 'learning_logs:index' %}">Learning Log</a> 
    <a href="{% url 'learning_logs:topics' %}">Topics</a> 
    {% if user.is_authenticated %}
        Hello, {{ user.username }}.
        <a href="{% url 'users:logout' %}">log out</a>
    {% else %}
        <a href="{% url 'users:login' %}">log in</a>
    {% endif %}
</p>

{% block content %}{% endblock content %}

I'm admittedly using an older tutorial and thus am sure the problem has to do with something that is in Django 2.0 but not in the older version that the book covers. Your help is greatly appreciated.

like image 233
Tom Avatar asked Dec 26 '17 20:12

Tom


2 Answers

Try to change your app_name='' in learning_logs/urls.py. (Create if there is not any.)

In the first learning_logs app you forgot s in the end (e.g. app_name = 'learning_logs')

like image 96
Begli Avatar answered Oct 06 '22 02:10

Begli


Set users as the value of the namespace keyword argument of include method to set the appropriate application namespace:

path('users/', include('users.urls', namespace='users'))
like image 30
heemayl Avatar answered Oct 06 '22 00:10

heemayl