【问题标题】:Django 1.5 - Cannot find the defined view when using TemplateViewDjango 1.5 - 使用 TemplateView 时找不到定义的视图
【发布时间】:2013-03-21 06:08:10
【问题描述】:

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

【问题讨论】:

  • 请不要在你得到答案后就这样删除你的问题,它可能对其他人有价值。您希望删除此问题的任何特殊原因?
  • @ShadowWizard 如果您指的是以下答案。它不工作。我删除了它,因为我知道为什么会出现这个错误。我最初放了def IndexView(TemplateView):,但正确答案是class IndexView(TemplateView):
  • @ShadowWizard 如果用户愿意,是否不允许用户删除自己的问题?如果有人已经正确回答,我不是那种删除某些东西的人。如果他的正确,我会标记它。
  • 如果您找到了有效的答案,请添加并接受。这样也可以帮助未来的用户。
  • Stack Overflow 不专注于个人帮助,而是为整个程序员社区做出贡献。如果您遇到问题并自己找到解决方案,请按照@Toon 的建议进行操作并分享。这样,这是一个双赢的局面。 :)

标签: django django-1.5


【解决方案1】:

您收到错误是因为您尚未将 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):

【讨论】:

  • 感谢@catherine。我认为您的问题完全正确,您没有理由尝试删除它。
  • 谢谢.. 我还有其他有效的问题。但我不知道为什么,很少有用户投反对票。我不知道如何让我的问题对他们有效。
  • 好好阅读常见问题解答 - 他们几乎定义了什么是有效问题 - stackoverflow.com/faq
  • practical, answerable questions based on actual problems that you face。但是为什么有些人不能承认我的问题
  • 有人否决了这个问题。我告诉过你我想删除它
【解决方案2】:

找出我的错误。我输入了def 而不是class

def IndexView(TemplateView): //wrong

class IndexView(TemplateView): //correct

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 2013-04-17
    • 1970-01-01
    • 2013-03-07
    • 2013-04-15
    • 1970-01-01
    • 2013-08-11
    • 2019-01-25
    • 2011-10-15
    相关资源
    最近更新 更多