Python 在 include() 中使用命名空间时关于 app_name 的 ImpropyConfiguredError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48608894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:46:44  来源:igfitidea点击:

ImpropyConfiguredError about app_name when using namespace in include()

pythondjangodjango-urls

提问by Nelson M

I am currently trying out django. I use the namespaceargument in one of my include()s in urls.py. When I run the server and try to browse, I get this error.

我目前正在尝试 django。我在 urls.pynamespace中的一个include()s 中使用了该参数。当我运行服务器并尝试浏览时,出现此错误。

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.

These are my urls.py files:

这些是我的 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)),
]

and

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

What do I pass the app_nameas stated in the error message?

我通过app_name错误消息中的说明传递了什么?

回答by unixia

Check the docs for include here.

检查包含此处的文档。

What you've done is not an acceptable way of passing parameters to include. You could do:

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

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

回答by Bob

Django 1.11+, 2.0+

Django 1.11+、2.0+

You should set the app_name in the urls file you are including

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

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

app_name = 'reviews'
?    

Then you can include it the way you are doing it.

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

Also, it might be worth noting what Django docs say here https://docs.djangoproject.com/en/1.11/ref/urls/#include:

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

Deprecated since version 1.9: Support for the app_name argument is deprecated and will be removed in Django 2.0. Specify the app_name as explained in URL namespaces and included URLconfs instead.

1.9 版后已弃用:不推荐使用 app_name 参数,并将在 Django 2.0 中删除。按照 URL 命名空间中的说明指定 app_name,并改为包含 URLconf。

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

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

回答by Brayan Loayza

Django 2.0 you should specify app_namein your urls.py, is not necessary to specify app_name argument on include.

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

Main Url file.

主 URL 文件。

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

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

Included Url.

包含网址。

from django.urls import path
from . import views

app_name = 'main_app'

urlpatterns = [
    path('', views.index, name='index'),
]

Then use use in template as

然后在模板中使用 use as

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

More details: https://code.djangoproject.com/ticket/28691Django 2.0 Docs

更多细节:https: //code.djangoproject.com/ticket/28691 Django 2.0 Docs

回答by Herbert

I included a library not (fully) django 2.1 compatible yet (django_auth_pro_saml2). Hence I create a second file saml_urls.py:

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

from django_saml2_pro_auth.urls import urlpatterns

app_name = 'saml'

Such that I could include the urls as:

这样我就可以将网址包括为:

from django.urls import include, re_path as url

urlpatterns = [
    ..., url(r'', include('your_app.saml_urls', namespace='saml')), ...
]

Hacky, but it worked for me, whereas the url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews'))did not.

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

回答by sayalok

I am also face the same error in Django 2.2and i solve it this way

我在Django 2.2 中也面临同样的错误,我是这样解决的

urls.py file

urls.py 文件

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

polls/urls.py file

polls/urls.py 文件

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

example use of namespace in calss based view method

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

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

example use of namespace in templates

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

{% url 'polls:index' %}

Here polls:indexmean app_name[define in polls/urls.py file]:name[define in polls/urls.py fileinside path function]

这里polls:index 的意思是 app_name[在polls/urls.py 文件中定义]:name[在polls/urls.py 文件中定义在path 函数中]

their official which is pretty good you can check for more info namespace_django_official_doc

他们的官方很不错,您可以查看更多信息namespace_django_official_doc

回答by coderanger

In my case, I was writing the urls outside the urlpatterns list. Please double check.

就我而言,我在 urlpatterns 列表之外编写了 url。请仔细检查。