我的URLs.py中有以下代码
from django.conf.urls import url, include, path
from django.contrib import admin
urlpatterns = [
path('moraimono/', include('moraimono.urls')),
path('admin/', admin.site.urls),
但我发现path()仅包含在Django 2.0中,而我使用的是Django 1.11b1。
如何修改此代码以使其在Django 1.11b1中工作
只需使用url()
而不是path()
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin', admin.site.urls),
url(r'^moraimono/', include('moraimono.urls')),
]
在url
文档的here中查找更多详细信息
使用url
,你已经导入了它,在django 2.0中它是re_path
的同义词
from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin
urlpatterns = [
url(r'^'moraimono/$', include('moraimono.urls')),
url(r'^'admin/$', admin.site.urls),
]
urlpatterns
应该是url()
实例的列表。 :
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^index/$', index_view, name='main-view'),
url(r'^moraimono/', include('moraimono.urls')),
...
]
url()
在内部定义为:
def url(regex, view, kwargs=None, name=None):
if isinstance(view, (list, tuple)):
# For include(...) processing.
urlconf_module, app_name, namespace = view
return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)
elif callable(view):
return RegexURLPattern(regex, view, kwargs, name)
else:
raise TypeError('view must be a callable or a list/tuple in the case of include().')