原文:https://blog.csdn.net/m0_38059875/article/details/82793269
《Python编程:从入门到实践》项目三 Web应用程序
书中原来的程序
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('learning_logs.urls', namespace='learning_logs')),
]
在Terminal中运行:
(ll_env) F:\Python\pycharm-workspace\learning_log>python manage.py runserver
运行报错:
File "F:\Python\pycharm-workspace\learning_log\lear
ning_log\urls.py", line 22, in
url(r'^admin/', include(admin.site.urls)),
File "F:\Python\pycharm-workspace\learning_log\ll_e
nv\lib\site-packages\django\urls\conf.py", line 27, in include
'provide the namespace argument to include() instead.' % len(arg)
django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. Pass a 2-tuple containing the list of patterns and app_name, and provide
the namespace argument to include() instead.
修改:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),# 不用include()函数
url(r'', include(('learning_logs.urls','learning_logs'), namespace='learning_logs')),
# url(r'', include('learning_logs.urls')), # 或者修改为include(pattern_list)这种用法也是可以的!!!
]
报错原因如下:
最后查找资料,Django2.1的官方文档。
根据文档的提示,admin.site.urls不用include()函数。书中第二个include()函数没有提供app_namespace,所以会报错!
修改好就可以正常运行了!