0

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

这是我的代码:

网址.py

from django.conf.urls import patterns, url

from . import views

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

视图.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
4

2 回答 2

2

您收到错误是因为您尚未导入IndexView当前命名空间。您已导入views. IndexView只能在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):

于 2013-03-21T06:43:26.563 回答
1

只要弄清楚我的错误。我把def而不是class.

def IndexView(TemplateView): //wrong

class IndexView(TemplateView): //correct
于 2013-03-21T09:57:50.860 回答