9

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.

Tom
  • 1,003
  • 2
  • 13
  • 25

6 Answers6

19

There are two ways can hanlded this.

Firstly,you can set an app_name attribute in the included URLconf module, at the same level as the urlpatterns attribute. You have to pass the actual module, or a string reference to the module, to include(), not the list of urlpatterns itself.

https://docs.djangoproject.com/en/2.0/topics/http/urls/#url-namespaces-and-included-urlconfs

urls.py

from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
]

polls/urls.py

from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    ...
]

have fun!

Ershan
  • 623
  • 8
  • 9
2

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')

APC
  • 144,005
  • 19
  • 170
  • 281
Begli
  • 21
  • 3
  • This is spot on. The question is based on an exercise in Python Crash Course, which omits the `app_name` . – APC Mar 22 '20 at 16:37
1

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'))
heemayl
  • 39,294
  • 7
  • 70
  • 76
1

You need to actually tell it what the namespace is.

Change this line:

path('users/', include('users.urls')),

to:

path('users/', include('users.urls', namespace="users")),

Read the documentation

ubadub
  • 3,571
  • 21
  • 32
1

Because the "learning_logs" URL namespaces includes the "users" one, they are considered to be nested. So when you want to find a URL inside "users" you need to use both names in your url tags:

<a href="{% url 'learning_logs:users:logout' %}">log out</a>
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

I had the same issue but managed to solve the issue by ensuring that project urls.py has a path to view that manages the app, in this case users.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Neel
  • 413
  • 1
  • 6
  • 14
  • The other answers go in the same direction. Can you please give an example? You can [edit] your answer to make it clearer. – Artjom B. May 04 '18 at 10:45