Django 2.0'名称'不是注册的名称空间 [英] Django 2.0 'name' is not a registered namespace

查看:92
本文介绍了Django 2.0'名称'不是注册的名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经多次看到此错误,但是找不到使用Django 2.0并出现问题的人。

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 : 

这是我的根urls.py

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

...和应用程序用户 url.py

... 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'),
]

和用户应用中的base.html

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公认使用的是旧版教程,因此可以确定问题与Django 2.0中的问题有关,而与本书所涵盖的旧版本中的问题无关。非常感谢您的帮助。

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.

推荐答案

有两种方法可以实现此目的。

There are two ways can hanlded this.

首先,您可以在包含的URLconf模块中设置 app_name 属性,其属性与 urlpatterns 属性。您必须将实际模块或对该模块的字符串引用传递给 include(),而不是 urlpatterns 本身。

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

民意调查/urls.py

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'),
    ...
]

玩得开心!

这篇关于Django 2.0'名称'不是注册的名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆