1

我正在尝试从基于 django 类的视图中执行 CRUD 应用程序。这是我更新/创建笔记的观点。

class CreateNoteView(CreateView):

    model = Note
    template_name = 'edit_note.html'

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

    def get_context_data(self):

        context = super(CreateNoteView, self).get_context_data(**kwargs)
        context['action'] = reverse('notes-create')
        return context


class UpdateNoteView(UpdateView):

    model = Note
    template_name = 'edit_note.html'

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

    def get_context_data(self, **kwargs):

        context = super(UpdateNoteView, self).get_context_data(**kwargs)
        context['action'] = reverse('notes-edit',
                                    kwargs={'pk':self.get_object().id})
        return context

这是我的模型文件:

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

    title = models.CharField(max_length=200)
    description = models.TextField()
    pub_date = models.DateField(auto_now_add=True)

我的网址文件如下所示:

urlpatterns = patterns('',
       url(r'^create/$',CreateNoteView.as_view(), name='notes-create',),
       url(r'^$', ListNoteView.as_view(), name='notes-list',),    
       url(r'^edit/(?P<pk>\d+)/$', UpdateNoteView.as_view(), name='notes-edit',),                       
)

模板如下:

{% if note.id %}
<h1> Edit Note </h1>
{% else %}
<h1>Add Notes</h1>
{% endif %}

<form action="{{ action }}" method="POST">
  {% csrf_token %}
  <ul>
    {{ form.as_p }}
  </ul>
  <input type="submit" value="Save" />
</form>
<a href="{% url "notes-list" %}">back to list</a>

我收到一个错误,

异常类型:/create/ 处的 TypeError

异常值:get_context_data() 得到了一个意外的关键字参数 'form'

这是回溯:

File "/home/.virtualenvs/djtalk/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/.virtualenvs/djtalk/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/.virtualenvs/djtalk/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/.virtualenvs/djtalk/local/lib/python2.7/site-packages/django/views/generic/edit.py" in get
  195.         return super(BaseCreateView, self).get(request, *args, **kwargs)
File "/home/.virtualenvs/djtalk/local/lib/python2.7/site-packages/django/views/generic/edit.py" in get
  155.         return self.render_to_response(self.get_context_data(form=form))

Exception Type: TypeError at /create/
Exception Value: get_context_data() got an unexpected keyword argument 'form'
4

2 回答 2

12

更新

def get_context_data(self):

CreateNoteView

def get_context_data(self, **kwargs):

当您覆盖一个方法时(在这种情况下:get_context_data),您应该确保尽可能保持相同的签名:在这种情况下,get_context_data预计会收到self, **kwargs.


关于您的评论:

class NoteMixin(object):

    model = Note
    template_name = 'edit_note.html'

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

     def get_context_data(self, **kwargs):
        context = super(NoteMixin, self).get_context_data(**kwargs)
        context['action'] = self.get_action()
        return context

class UpdateNoteView(NoteMixin, UpdateView):
    def get_action(self):
        return reverse('notes-edit', kwargs={'pk':self.get_object().id})

class CreateNoteView(NoteMixin, CreateView):
    def get_action(self):
        return reverse('notes-create')
于 2013-04-19T18:02:49.653 回答
1

检查您的 CreateNoteView.get_context_data 函数签名 - /create/ 映射到您的 urls.py 中的 CreateNoteView

于 2013-04-19T17:58:41.050 回答