Nemo
Nemo
Django避坑指南
Django避坑指南

1. pycharm下Django项目,配置路由时报错:include() got an unexpected keyword argument ‘app_name’

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41574947/article/details/91614681
————————————————
报错源码:
urlpatterns = [
path(‘admin/’, admin.site.urls),
#path(’’, include(‘stu.urls’)),
#增加命名空间函数
path(’’,include(‘stu.urls’, namespace=‘stuapp’,app_name=‘stu’)),
]
https://kanghaov-img-1256185664.file.myqcloud.com/2019/09/18/789ff25a909ab.png

1、错误原因
Django2.x版本中include只有两个参数,app_name为多余的关键字参数,不写仍然报错。

https://kanghaov-img-1256185664.file.myqcloud.com/2019/09/18/762b269656648.png

2、解决方法:
删除跟路由中include下的app_name=“stu”,同时在子路由app路由中加入app_name=“stu”。

https://kanghaov-img-1256185664.file.myqcloud.com/2019/09/18/ba5e2745a353b.png

https://kanghaov-img-1256185664.file.myqcloud.com/2019/09/18/9c56619ca4371.png

2.TypeError: include() got an unexpected keyword argument ‘app_name’

如果在项目文件夹的urls.py中设置相关路由到对应的app中,老版本的代码为:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^blog/',include('blog.urls',namespace='blog',app_name = 'blog')),
    url(r'^account/',include('account.urls',namespace='account',app_name='account')),

]

在运行服务的时候会报错:

TypeError: include() got an unexpected keyword argument 'app_name'

解决办法是把app_name参数分别放到对应app应用的urls.py文件中:

  • https://kanghaov-img-1256185664.file.myqcloud.com/2019/09/23/0261d797ca7c6.jpg
  • https://kanghaov-img-1256185664.file.myqcloud.com/2019/09/23/28cc421dd6b41.jpg

这样没问题了。

Nemo版权所有丨如未注明,均为原创丨本网站采用BY-NC-SA协议进行授权,转载请注明转自:https://kanghaov.com/385.html
https://secure.gravatar.com/avatar/9fd8359b8faa6f7789f9623ba6041e4a?s=256&d=identicon&r=g

kanghaov

文章作者

发表评论

textsms
account_circle
email

Nemo

Django避坑指南
1. pycharm下Django项目,配置路由时报错:include() got an unexpected keyword argument 'app_name' 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处…
扫描二维码继续阅读
2019-09-18