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