如何在Django 1.9中传递callable

时间:2015-12-08 14:59:23

标签: python django django-models django-urls django-1.9

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

public static byte[] ReadStream(Stream responseStream)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

每次都会收到错误消息:

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'),
]

但我无法找到如何通过它们。文件只告诉他们传递他们的信息。但没有例子如何...

1 个答案:

答案 0 :(得分:20)

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

即可
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(r'^rezept/(?P<slug>[-\w]+)/$', recipes_views.detail),
url(r'^$', recipes_views.index),

More documentation and the reasoning can be found here

  

在现代,我们更新了教程而不是推荐   导入视图模块并引用视图函数(或   课程直接。这具有许多优点,都源于此   事实上,我们使用普通的Python代替“Django String   Magic“:错误输入视图名称时的错误不那么模糊,IDE   可以帮助自动完成视图名称等。