不推荐使用对url()的字符串视图参数的支持,并且将在Django 1.10中删除它。传递可调用的

时间:2016-04-14 10:47:24

标签: python django

这是我发出警告的代码。

它会在"posts.views.post_list"中为urlpatterns等字符串发出警告,并建议使用而不是字符串来传递可调用字符串。我需要修复或删除该警告。

from django.conf.urls import url
#from django.contrib import admin

from . import views

urlpatterns = [
   url(r'^$', "posts.views.post_list"),
   url(r'^create/$', "posts.views.post_create"),
   url(r'^detail/$', "posts.views.post_detail"),
   url(r'^update/$', "posts.views.post_update"),
   url(r'^delete/$', "posts.views.post_delete"),    
   #url(r'^posts/$', "<app_name>.<views_py>.<function_name>"),
]

3 个答案:

答案 0 :(得分:2)

谢谢大家,我自己解决了这个警告。现在它运行正常,没有发出任何警告。

from django.conf.urls import url
#from django.contrib import admin

from .views import(
  post_list,
  post_create,
  post_detail,
  post_update,
  post_delete
)

urlpatterns = [
  url(r'^$', post_list),
  url(r'^create/$', post_create),
  url(r'^detail/$', post_detail),
  url(r'^update/$', post_update),
  url(r'^delete/$', post_delete),    
  #url(r'^posts/$', "<app_name>.<views_py>.<function_name>"),
]

答案 1 :(得分:1)

如警告所示,不推荐将字符串传递给urldocs)。

你必须传递callable。这意味着导入函数并直接传递它:

 urlpatterns = [
   url(r'^$', views.post_list),
   url(r'^create/$', views.post_create),
   url(r'^detail/$', views.post_detail),
   url(r'^update/$', views.post_update),
   url(r'^delete/$', views.post_delete),    
]

答案 2 :(得分:1)

您应该传递真实函数,而不是包含函数名称的字符串:

from django.conf.urls import url
#from django.contrib import admin

from . import views

urlpatterns = [
   url(r'^$', views.post_list),
   url(r'^create/$', views.post_create),
   url(r'^detail/$', views.post_detail),
   url(r'^update/$', views.post_update),
   url(r'^delete/$', views.post_delete),    
   #url(r'^posts/$', <views_py>.<function_name>),
]