[英]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
什么?
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.