我创建 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