Django新用户:NameError,即使在视图中定义了类

时间:2013-04-05 18:11:44

标签: django django-models django-views

我是python的新手,甚至更新的django。我目前正在关注effectivedjango教程。我的项目文件夹具有以下结构。

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>

视图:

# 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

模型:

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,
        ])

网址

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)),
)

当我尝试通过virtualenv从开发服务器运行它时,我得到以下错误:

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

我以为我在视图中定义了UpdateView,所以我真的不知道我做错了什么。任何建议将不胜感激。

感谢

1 个答案:

答案 0 :(得分:1)

在您的观看中,您使用ListViewCreateViewUpdateView,但只能导入ListViewCreateView

<强> views.py:

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