gpt4 book ai didi

python - 基于 django 类的 View get_context_data 得到了一个意外的关键字参数

转载 作者:太空宇宙 更新时间:2023-11-04 06:52:55 26 4
gpt4 key购买 nike

我正在尝试从基于 Django 类的 View 中创建一个 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)

我的 urls 文件如下所示:

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>

我收到一个错误,

异常类型:TypeError at/create/

异常值: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'

最佳答案

更新

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')

关于python - 基于 django 类的 View get_context_data 得到了一个意外的关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16110534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com