Django 2.0 'name' is not a registered namespace

前端 未结 6 2410
别那么骄傲
别那么骄傲 2021-02-14 17:46

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

相关标签:
6条回答
  • 2021-02-14 18: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!

    0 讨论(0)
  • 2021-02-14 18:20

    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'))
    
    0 讨论(0)
  • 2021-02-14 18:25

    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

    0 讨论(0)
  • 2021-02-14 18:36

    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>
    
    0 讨论(0)
  • 2021-02-14 18:36

    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.

    0 讨论(0)
  • 2021-02-14 18:41

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

    0 讨论(0)
提交回复
热议问题