【发布时间】:2016-11-19 03:48:32
【问题描述】:
from django.conf.urls import url, patterns, include
from django.contrib import admin
from django.views.generic import TemplateView
from collection import *
#from collection.views import index,thing_detail,edit_thing
urlpatterns = [
url(r'^$', views.index, name='home'),
url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
url(r'^contact/$',TemplateView.as_view(template_name='contact.html'),name='contact'),
url(r'^things/(?P<slug>[-\w]+)/$', 'views.thing_detail' ,name='thing_detail'),
url(r'^things/(?P<slug>[-\w]+)/edit/$', 'views.edit_thing',name='edit_thing'),
url(r'^admin/', include(admin.site.urls)),
]
运行服务器后出现错误“NameError: name 'views' is not defined”
有什么帮助吗??
【问题讨论】:
-
您没有导入自己的视图
-
如果
collection是您的app名称,我建议将from collection import *更改为from collection import views,具体view是什么 -
首先明确导入您的视图。还要避免在 url (
'views.edit_thing') 中使用字符串,因为它会引发弃用警告并且也不是一个好习惯。最后使用可调用视图本身edit_thing而不是views.edit_thing。
标签: django python-2.7 django-views