如何在 Django 1.9 中传递 callable

How to pass callable in Django 1.9

大家好,我是 Python 和 Django 的新手,我遵循 django workshop 指南。 我刚刚安装了 Python 3.5 和 Django 1.9 并收到很多错误消息...... 刚才我发现了很多文档,但现在卡住了。 我想添加视图,所以我在 urls.py:

中添加了以下代码
from django.conf.urls import include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    # Uncomment the admin/doc line below to enable admin documentation:
    #url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^rezept/(?P<slug>[-\w]+)/$', 'recipes.views.detail'),
    url(r'^$', 'recipes.views.index'),
]

并且每次都收到错误信息:

Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got recipes.views.index). Pass the callable instead.
  url(r'^$', 'recipes.views.index'),

但我找不到如何通过它们。文档只告诉 "pass them" 但没有示例如何...

这是一个弃用警告,这意味着代码现在仍然 运行。但要解决这个问题,只需更改

url(r'^$', 'recipes.views.index'),

对此:

#First of all explicitly import the view
from recipes import views as recipes_views #this is to avoid conflicts with other view imports

并且在 URL 模式中,

url(r'^rezept/(?P<slug>[-\w]+)/$', recipes_views.detail),
url(r'^$', recipes_views.index),

More documentation and the reasoning can be found here

In the modern era, we have updated the tutorial to instead recommend importing your views module and referencing your view functions (or classes) directly. This has a number of advantages, all deriving from the fact that we are using normal Python in place of “Django String Magic”: the errors when you mistype a view name are less obscure, IDEs can help with autocompletion of view names, etc.