基于django类的视图get_context_data得到了一个意外的关键字参数

时间:2013-04-19 17:43:33

标签: python django django-forms django-class-based-views

我正在尝试从基于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'

2 个答案:

答案 0 :(得分:10)

更新

def get_context_data(self):

CreateNoteView中:

def get_context_data(self, **kwargs):

当你覆盖某个方法时(在这种情况下:get_context_data),你应该确保在可能的情况下保留相同的签名:在这种情况下,get_context_data应该会收到{{1} }}


关于你的评论:

self, **kwargs

答案 1 :(得分:1)

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