嗨,我是Python和Django的新手,我遵循django研讨会指南.我刚安装了Python 3.5和Django 1.9并得到了很多错误消息......刚才我发现了很多dokumentations但现在卡住了.我想添加视图,所以我在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[-\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'),
但我找不到如何通过它们.文件只告诉"通过他们",但没有例子如何......
这是一个弃用警告,这意味着代码现在仍然可以运行.但要解决这个问题,只需要改变
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[-\w]+)/$', recipes_views.detail), url(r'^$', recipes_views.index),
可以在此处找到更多文档和推理
在现代,我们已经更新了教程,而是建议导入视图模块并直接引用视图函数(或类).这有很多优点,都源于我们使用普通的Python代替"Django String Magic"这一事实:错误输入视图名称时的错误不那么模糊,IDE可以帮助自动完成视图名称等.