Nel*_*n M 82 python django django-urls
我正在尝试django.我在urls.py中使用了namespace我include()的一个参数.当我运行服务器并尝试浏览时,我收到此错误.
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.
Run Code Online (Sandbox Code Playgroud)
这些是我的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)),
]
Run Code Online (Sandbox Code Playgroud)
和
#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'),
]
Run Code Online (Sandbox Code Playgroud)
我如何传递app_name错误消息中所述的内容?
uni*_*xia 122
在此处查看包含的文档.
您所做的不是传递参数以包含的可接受方式.你可以这样做:
url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews')),
Run Code Online (Sandbox Code Playgroud)
Bob*_*Bob 66
您应该在包含的urls文件中设置app_name
# reviews/urls.py <-- i.e. in your app's urls.py
app_name = 'reviews'
Run Code Online (Sandbox Code Playgroud)
然后你可以按照你的方式包含它.
此外,值得注意Django文档在这里说的是什么https://docs.djangoproject.com/en/1.11/ref/urls/#include:
从版本1.9开始不推荐使用:不推荐使用对app_name参数的支持,将在Django 2.0中删除.按照URL命名空间中的说明指定app_name,并包含URLconf.
(https://docs.djangoproject.com/en/1.11/topics/http/urls/#namespaces-and-include)
Bra*_*yza 24
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),
]
Run Code Online (Sandbox Code Playgroud)
包含的网址.
from django.urls import path
from . import views
app_name = 'main_app'
urlpatterns = [
path('', views.index, name='index'),
]
Run Code Online (Sandbox Code Playgroud)
然后在模板中使用as
<a href="{% url main_app:index' %}"> link </a>
Run Code Online (Sandbox Code Playgroud)
更多细节:https ://code.djangoproject.com/ticket/28691 Django 2.0 Docs
| 归档时间: |
|
| 查看次数: |
45974 次 |
| 最近记录: |