0

I create index.html and I use TemplateView. When I'm trying to access my index, I got an error that my index view is not defined. I don't know why, I just follow this pattern https://docs.djangoproject.com/en/dev/topics/class-based-views/#subclassing-generic-views

Here are my codes:

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'

traceback (update)

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
catherine
  • 22,492
  • 12
  • 61
  • 85
  • Please don't delete your questions just like that after you get answer, it might have value to other people. Any special reason why you want this question to be deleted? – Shadow The GPT Wizard Mar 21 '13 at 09:35
  • @ShadowWizard If you are referring about the answer below. It's not working. I deleted it because I figure out why I get that error. I originally put `def IndexView(TemplateView):` but the correct answer is `class IndexView(TemplateView):` – catherine Mar 21 '13 at 09:40
  • @ShadowWizard Is SO don't permit user to delete their own question, if they want? I'm not kind of person to delete something if someone has already answer it correctly. If his correct, I will mark it. – catherine Mar 21 '13 at 09:44
  • If you found a working answer, please add it and accept it. That way future users can be helped too. – Toon Krijthe Mar 21 '13 at 09:45
  • 2
    Stack Overflow is not focused on personal help but rather *contributing to the programmers community as a whole*. If you had a problem and found the solution yourself do as @Toon suggested and share it. This way it's a win-win situation. :) – Shadow The GPT Wizard Mar 21 '13 at 09:47
  • @ShadowWizard Ok I will post an answer. I just want to delete it because it just one line code. And I also noticed users here, if they find it very simple they down voted it. I always down voted because my question are just for beginners. – catherine Mar 21 '13 at 09:54
  • @catherine, my answer was correct. It fixed the Exception in your original question. I see that you have an additional error, which throws a new exception. I've updated my answer to address both errors. Can I suggest that you revert to the original version of your question. It was a valid question and may be useful to others. Your current revision (no 3) is wrong - the code throws a NameError not an AttributeError. – Aidan Ewen Mar 21 '13 at 11:45
  • @AidanEwen Yeah I updated it and back to my original codes. And before you updated your answer I already fix it that's why I try to delete it a while ago – catherine Mar 21 '13 at 11:48

2 Answers2

2

You're getting the error because you haven't imported IndexView into the current namespace. You've imported views. IndexView is only accessible within the views namespace - ie views.IndexView.

You should re-write your import statement -

from yourapp.views import IndexView

Or else re-write your urlconf -

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

Maybe have a read on python's namespaces - that's what's causing your NameError exception.

On top of that, you've also defined your view as a function instead of a class it should be -

class IndexView(TemplateView): rather than def IndexView(TemplateView):

Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
  • thanks @catherine. I think your question is perfectly valid, there is no reason for you to try and delete it. – Aidan Ewen Mar 21 '13 at 11:34
  • Thank you.. I have also other questions that are valid. But I don't know why, there are few users, who down voted it. I don't know how to make my question valid for them. – catherine Mar 21 '13 at 11:53
  • Have a good read of the faq's - they pretty much define what a valid question is - http://stackoverflow.com/faq – Aidan Ewen Mar 21 '13 at 12:11
  • `practical, answerable questions based on actual problems that you face`. But why there are some people who cannot acknowledge my question – catherine Mar 21 '13 at 12:21
  • Someone down voted this question. I told you I want to delete this instead – catherine Mar 21 '13 at 12:45
1

Just figure out my mistake. I put def instead of class.

def IndexView(TemplateView): //wrong

class IndexView(TemplateView): //correct
catherine
  • 22,492
  • 12
  • 61
  • 85