【问题标题】:ImproperlyConfiguredError about app_name when using namespace in include()在 include() 中使用命名空间时有关 app_name 的错误配置错误
【发布时间】:2018-07-14 12:02:47
【问题描述】:

我目前正在试用 Django。我在 urls.py 中的 include()s 之一中使用了 namespace 参数。当我运行服务器并尝试浏览时, 我收到此错误。

File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\conf.py", line 39, in include
    'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

这些是我的 urls.py 文件:

#project/urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^reviews/', include('reviews.urls', namespace='reviews')),
    url(r'^admin/', include(admin.site.urls)),
]

#app/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /
    url(r'^$', views.review_list, name='review_list'),
    # ex: /review/5/
    url(r'^review/(?P<review_id>[0-9]+)/$', views.review_detail, name='review_detail'),
    # ex: /wine/
    url(r'^wine$', views.wine_list, name='wine_list'),
    # ex: /wine/5/
    url(r'^wine/(?P<wine_id>[0-9]+)/$', views.wine_detail, name='wine_detail'),
]

如错误消息中所述,我该如何传递app_name

【问题讨论】:

    标签: python django django-urls


    【解决方案1】:

    检查文档是否包含 here

    您所做的传递参数以包含的方式是不可接受的。你可以这样做:

    url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews')),
    

    【讨论】:

    • 刚刚试了一下,又出现了一个错误...url(r'^reviews/', include('reviews.urls', namespace='reviews', app_name='reviews')), TypeError: include() 得到了一个意外的关键字参数 'app_name'
    • 非常抱歉。那应该与模块而不是模式一起使用。请检查更新的答案。
    • 这个解决方案对我也很有帮助。我正在遵循django-oauth-toolkit.readthedocs.io/en/latest/tutorial/… 的教程,并且有一条指令要做:urlpatterns = [ # OAuth 2 endpoints: url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider")), url(r' ^api/hello', ApiEndpoint.as_view()), # 一个示例资源端点]。这会产生与您提到的相同的错误。
    • 元组语法对我不起作用,它仅在我将(我的版本)reviews.url 更改为在app_name 中有一个app_name 变量时才起作用django 2.1
    • 这个的用例是什么?我的项目中有很多包含的 url,并且都具有相同的模式:path("the_app/", include(("app.urls", &lt;name&gt;), namespace=&lt;the same name&gt;)),这很烦人。
    【解决方案2】:

    Django 1.11+、2.0+

    您应该在包含的 urls 文件中设置 app_name

    # reviews/urls.py  <-- i.e. in your app's urls.py
    
    app_name = 'reviews'
         
    

    然后你可以按照你的方式包含它。

    此外,Django 文档在https://docs.djangoproject.com/en/1.11/ref/urls/#include 中所说的内容可能值得注意:

    自 1.9 版起已弃用:对 app_name 参数的支持是 已弃用并将在 Django 2.0 中删除。将 app_name 指定为 在 URL 命名空间中进行了解释,并改为包含 URLconfs。

    (https://docs.djangoproject.com/en/1.11/topics/http/urls/#namespaces-and-include)

    【讨论】:

      【解决方案3】:

      Django 2.0 你应该在 urls.py 中指定 app_name,不需要在 include 中指定 app_name 参数。

      主 URL 文件。

      from django.contrib import admin
      from django.urls import path, include
      
      urlpatterns = [
          path('', include('apps.main.urls')),
          path('admin/', admin.site.urls),
      ]
      

      包含的网址。

      from django.urls import path
      from . import views
      
      app_name = 'main_app'
      
      urlpatterns = [
          path('', views.index, name='index'),
      ]
      

      然后在模板中使用 use as

      <a href="{% url main_app:index' %}"> link </a>
      

      更多详情:https://code.djangoproject.com/ticket/28691 Django 2.0 Docs

      【讨论】:

      • 我想知道,app_name = "main" 有什么用?以及 django 如何使用这个变量
      【解决方案4】:

      我包含了一个尚未(完全)兼容 django 2.1 的库(django_auth_pro_saml2)。因此我创建了第二个文件saml_urls.py:

      from django_saml2_pro_auth.urls import urlpatterns
      
      app_name = 'saml'
      

      这样我就可以包含以下网址:

      from django.urls import include, re_path as url
      
      urlpatterns = [
          ..., url(r'', include('your_app.saml_urls', namespace='saml')), ...
      ]
      

      Hacky,但它对我有用,而 url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews')) 没有。

      【讨论】:

        【解决方案5】:

        我在 Django 2.2 中也面临同样的错误,我通过这种方式解决了它

        urls.py 文件

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

        polls/urls.py 文件

        app_name = 'polls'
        urlpatterns = [
          path('', views.IndexView.as_view(), name='index')
        ]
        

        在基于类的视图方法中使用命名空间的示例

        def get_absolute_url(self):
            from django.urls import reverse
            return reverse('polls.index', args=[str(self.id)])
        

        模板中命名空间的使用示例

        {% url 'polls:index' %}
        

        这里 polls:index 表示 app_name[在 polls/urls.py 文件中定义]:name[在 polls/urls.py 文件中定义 > 内部路径函数]

        他们的官方非常好,您可以查看更多信息namespace_django_official_doc

        【讨论】:

          【解决方案6】:

          在APP(reviews)目录中找到未使用的文件或目录,并检测该项目!

          【讨论】:

            猜你喜欢
            • 2016-04-01
            • 2018-07-07
            • 2020-05-19
            • 2018-12-03
            • 1970-01-01
            • 2020-04-10
            • 2013-11-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多