Django 1.11中path()的替代方案

时间:2018-01-19 09:18:12

标签: python django

我的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中运行

3 个答案:

答案 0 :(得分: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

中查找更多详细信息

答案 1 :(得分: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().')

答案 2 :(得分:0)

使用RewriteEngine On RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^ - [L] RewriteRule ^ index.php RewriteCond %{HTTP_HOST} ^em.example.com/lib/view/map$ [NC] RewriteRule ^pathurl/(.*)$ /em.example.com/map/$1 [R=301,NC,L] ,您已将其导入,而在django 2.0中,它与url

同义
re_path