0

我是 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,所以我真的不知道我做错了什么。任何建议将不胜感激。

谢谢

4

1 回答 1

1

在您的观点中,您使用ListView,CreateViewUpdateView,但只导入ListViewCreateView

视图.py:

from django.views.generic import ListView, CreateView, UpdateView
from django.core.urlresolvers import reverse
from contacts.models import Contact
import forms
...
于 2013-04-05T18:14:02.393 回答