简体   繁体   English

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

[英]ImproperlyConfiguredError about app_name when using namespace in include()

I am currently trying out Django. I use the namespace argument in one of my include() s in urls.py.我目前正在试用 Django。我在 urls.py 中的一个include()中使用namespace参数。 When I run the server and try to browse, I get this error.当我运行服务器并尝试浏览时,出现此错误。

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_name as stated in the error message?我应该按照错误消息中的说明传递app_name什么?

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

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. 1.9 版后已弃用:不推荐使用 app_name 参数,并将在 Django 2.0 中删除。 Specify the app_name as explained in URL namespaces and included URLconfs instead.按照 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

Django 2.0 you should specify app_name in 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/28691 Django 2.0 Docs更多细节: https : //code.djangoproject.com/ticket/28691 Django 2.0 Docs

I included a library not (fully) django 2.1 compatible yet (django_auth_pro_saml2).我包含了一个不(完全)与 django 2.1 兼容的库(django_auth_pro_saml2)。 Hence I create a second file saml_urls.py :因此我创建了第二个文件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'))没有。

I am also face the same error in Django 2.2 and 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:index mean app_name[define in polls/urls.py file ]:name[define in polls/urls.py file inside 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

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

In project level urls.py在项目级别 urls.py

path('',include('appname.urls'), namespace= 'test')

Now in in your app level urls.py现在在您的应用程序级别 urls.py

app_name = 'test'
path('',views.index, name= 'home')

Long story short is, You have to provide "app_name" in your app level urls and that same name should be your namespace name at project level url长话短说,您必须在应用程序级别的网址中提供“app_name”,并且该名称应该是项目级别的命名空间名称 url

In my case, I was writing the urls outside the urlpatterns list. 就我而言,我在urlpatterns列表之外写了url。 Please double check. 请仔细检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不支持在 include() 中指定命名空间而不提供 app_name - Specifying a namespace in include() without providing an app_name is not supported Django 2名称空间和app_name - Django 2 namespace and app_name 在 include() 中指定命名空间而不提供 app_name。 给 app_name 也不起作用 - Specifying a namespace in include() without providing an app_name. Giving app_name also doesn't work Django social_django&#39;在include()中指定名称空间而不提供app_name&#39; - Django social_django 'Specifying a namespace in include() without providing an app_name ' 使用Django时出现SyntaxError app_name - SyntaxError app_name while using Django 为什么我会收到“CommandError: App &#39;app_name&#39; 没有迁移。” 什么时候使用 Heroku? - Why do I get “CommandError: App 'app_name' does not have migrations.” when using Heroku? 如何在 Django 中包含带有 app_name 的 url,而不必使用带有应用名称的反向? - How to include urls with app_name without having to use reverse with app name in Django? 未命名模块<app_name> - No module named <app_name> 与/ app_name / page或app_name / page混淆 - Confused with /app_name/page or app_name/page 在没有app_name的情况下url.py中的include关键字不起作用(Django 2.0) - Include keyword in url.py not working without app_name(Django 2.0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM