Django 1.5 - 使用TemplateView时找不到定义的视图

时间:2013-03-21 06:08:11

标签: django django-1.5

我创建了index.html并使用了TemplateView。当我试图访问我的索引时,我得到一个错误,我的索引视图没有定义。我不知道为什么,我只是遵循这种模式 https://docs.djangoproject.com/en/dev/topics/class-based-views/#subclassing-generic-views

以下是我的代码:

urls.py

from django.conf.urls import patterns, url

from . import views

urlpatterns = patterns('',
    url(
        r'^$', 
        views.IndexView.as_view(),
        name='index'
    ),
)

views.py

from django.views.generic import TemplateView

def IndexView(TemplateView):
    template_name = 'index.html'

追溯(更新)

Environment:


Request Method: GET
Request URL: http://localhost:8000/

Django Version: 1.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'accounts',
 'front')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/cath/virtualenvs/autovine_env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/home/cath/virtualenvs/autovine_env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/home/cath/virtualenvs/autovine_env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/cath/virtualenvs/autovine_env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/home/cath/virtualenvs/autovine_env/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/cath/src/autovine/autovine/urls.py" in <module>
  33.         include('front.urls', namespace="front")
File "/home/cath/virtualenvs/autovine_env/local/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
  25.         urlconf_module = import_module(urlconf_module)
File "/home/cath/virtualenvs/autovine_env/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/cath/src/autovine/autovine/apps/front/urls.py" in <module>
  8.         IndexView.as_view(),

Exception Type: NameError at /
Exception Value: name 'IndexView' is not defined

2 个答案:

答案 0 :(得分:2)

您收到错误是因为您尚未将IndexView导入当前命名空间。您已导入viewsIndexView只能在views命名空间内访问 - 即views.IndexView

您应该重写您的导入声明 -

from yourapp.views import IndexView

或者重写你的urlconf -

urlpatterns = patterns(' ',
   url(r'^$', views.IndexView.as_view(), name='index'), 
)

也许读过python的命名空间 - 这就是导致NameError异常的原因。

除此之外,您还将视图定义为函数而不是应该是类 -

class IndexView(TemplateView):而不是def IndexView(TemplateView):

答案 1 :(得分:1)

弄清楚我的错误。我将def代替class

def IndexView(TemplateView): //wrong

class IndexView(TemplateView): //correct