New url format in Django 1.9

心不动则不痛 提交于 2019-12-03 15:04:37

问题


I recently upgraded my Django project to version 1.9.

When I try to run migrate, I am getting the following two errors:

  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.

Could someone please show me the proper syntax of how to do this? A brief sample of my urls.py is below:

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

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

Thank you!


回答1:


Import your views directly, or your views modules:

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

Do not use patterns at all, just use a list or tuple:

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

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



回答2:


You should remove the quotes around views name. So your code will be like that

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

Point 2, use lists, so your code will transform to

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


来源:https://stackoverflow.com/questions/34108321/new-url-format-in-django-1-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!