Rudy
Rudy

Reputation: 25

Django new user: NameError even though class is defined in views

I am new to python and even newer to django. i am currently following effectivedjango tutorial . my project folder has the following structure.

addressbook
  - addressbook
      - _init_.py
      - settings.py
      - urls.py
      - wsgi.py
      - _init_.pyc
      - settings.pyc
      - urls.pyc
      - wsgi.pyc

  - contacts
      - forms.py
      - _init_.py
      - models.py
      - tests.py
      - views.py
      - forms.pyc
      - _init_.pyc
      - models.pyc
      - tests.pyc
      - views.pyc
      - templates
            - contact_list.html
            - edit_contact.html</li>

VIEWS:

# Create your views here.

from django.views.generic import ListView
from django.core.urlresolvers import reverse
from django.views.generic import CreateView
from contacts.models import Contact
import forms

class ListContactView(ListView):

    model = Contact
    template_name = 'contact_list.html'

class CreateContactView(CreateView):

    model = Contact
    template_name = 'edit_contact.html'
    form_class = forms.ContactForm

    def get_success_url(self):
        return reverse('contacts-list')

class UpdateContactView(UpdateView):

    model = Contact
    template_name = 'edit_contact.html'
    form_class = forms.ContactForm

MODELS:

from django.db import models

# Create your models here.
class Contact(models.Model):

    first_name = models.CharField(
        max_length=255,
    )
    last_name = models.CharField(
        max_length=255,

    )

    email = models.EmailField()

    def __str__(self):

        return ' '.join([
            self.first_name,
            self.last_name,
        ])

URLs

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

import contacts.views

urlpatterns = patterns('',url(r'^$', contacts.views.ListContactView.as_view(),
        name='contacts-list',),
url(r'^new$', contacts.views.CreateContactView.as_view(),
    name='contacts-new',),

    # Examples:
    # url(r'^$', 'addressbook.views.home', name='home'),
    # url(r'^addressbook/', include('addressbook.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

When I try to run this from the development server through virtualenv i get the following errors:

Traceback:
...
File "/home/rudresh/tutorial/addressbook/addressbook/urls.py" in <module>
  7. import contacts.views
File "/home/rudresh/tutorial/addressbook/contacts/views.py" in <module>
  23. class UpdateContactView(UpdateView):

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

I thought i defined UpdateView in views so I dont really know what I am doing wrong. Any suggestion will be appreciated.

thanks

Upvotes: 0

Views: 1976

Answers (1)

Pavel Anossov
Pavel Anossov

Reputation: 62938

In your views, you use ListView, CreateView and UpdateView, but only import ListView and CreateView.

views.py:

from django.views.generic import ListView, CreateView, UpdateView
from django.core.urlresolvers import reverse
from contacts.models import Contact
import forms
...

Upvotes: 1

Related Questions