Django 1.9中的新url格式

时间:2015-12-05 17:11:09

标签: django django-urls django-1.9

我最近将我的Django项目升级到版本1.9。

当我尝试运行migrate时,我收到以下两个错误:

  1. Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got app.views.about). Pass the callable instead.
  2. django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.
  3. 有人可以告诉我如何做到这一点的正确语法吗?我的urls.py的简短示例如下:

    urlpatterns = patterns('',
        url(r'^about/$', 'app.views.about',
            name='about'),
    )
    
    urlpatterns += patterns('accounts.views',
        url(r'^signin/$', 'auth_login',
            name='login'),
    )
    

    谢谢!

2 个答案:

答案 0 :(得分:26)

直接导入视图或视图模块:

from apps.views import about
from accounts import views as account_views

根本不要使用patterns,只需使用列表或元组:

urlpatterns = [
    url(r'^about/$', about,
        name='about'),
]

urlpatterns += [
    url(r'^signin/$', account_views.auth_login,
        name='login'),
]

答案 1 :(得分:4)

您应该删除视图名称周围的引号。 所以你的代码就像那样

urlpatterns = patterns('',
    url(r'^about/$', app.views.about, #without quote!
        name='about'),
)

第2点,使用列表,因此您的代码将转换为

urlpatterns = [
        url(r'^about/$', app.views.about, #without quote!
            name='about'),
    ]