Django 1.11中path()的替代方案

问题描述 投票:2回答:3

我的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中工作

python django
3个回答
2
投票

只需使用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中查找更多详细信息


0
投票

使用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),
]

0
投票

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().')
© www.soinside.com 2019 - 2024. All rights reserved.