参考:https://blog.csdn.net/weixin_40841752/article/details/79335345
在学习《Python编程:从入门到实践》18.3.1 映射URL,遇到下面的问题:原著里使用的是1.x版本的django,本人使用的是2.1.7版本的django。而include()在1.x和2.x版本里使用方式是有区别的,照搬书上的代码,在执行python3 manage.py runserver可能会遇到下面的错误。另外2.x版本里推荐使用path替代url。
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.
传递一个3元组给include()是不支持的。
传递一个2元组(包含patterns和app_name的列表),
并且提供namespace实参给include()替代。
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.
在include方法里面指定namespace却不提供app_name是不支持的。
在包含的模块里设置app_name变量,
或者在include方法里面提供app_name参数。
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('learning_logs.urls',namespace='learning_logs')),
]
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
from django.conf.urls import url, include
#from django.urls import path, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
#path('admin/', admin.site.urls),
url(r'', include(('learning_logs.urls', 'LL'), namespace='learning_logs')),
#path('', include(('learning_logs.urls', 'LL'), namespace='learning_logs')),
]
from django.conf.urls import url
#from django.urls import path
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
#path('', views.index, name='index'),
]
from django.conf.urls import url, include
#from django.urls import path, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
#path('admin/', admin.site.urls),
url(r'', include('learning_logs.urls', namespace='learning_logs')),
#path('', include('learning_logs.urls', namespace='learning_logs')),
]
from django.conf.urls import url
#from django.urls import path
from . import views
app_name='LL'
urlpatterns = [
url(r'^$', views.index, name='index'),
#path('', views.index, name='index'),
]