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

URL

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

ビューでは、 と を使用しますがListViewCreateViewUpdateViewのみをインポート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 に答える