我有一个CBV:
class Publish(CreateView):
success_url = '/'
template_name = "article/publish.html"
form_class = ArticlePublish
model = Article
def get(self, request, *args, **kwargs):
print('ee')
return super(Publish, self).get(request)
def post(self, request, *args, **kwargs):
print('jj')
return super(Publish, self).post(request)
def form_valid(self, form):
print('dd')
form.instance.author = User.objects.get(id=self.request.user.id)
return super(Publish, self).form_valid(form)
def form_invalid(self, form):
print('cc')
return super(Publish, self).form_invalid(form)
当我提交此表格时:
class ArticlePublish(forms.ModelForm):
class Meta:
model = Article
fields = ['title', 'synopsis', 'content']
使用以下HTML:
<form action="" method='POST'>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>
我明白了:
[13/Oct/2016 16:33:53] "GET /publish/ HTTP/1.1" 200 591
ee
[13/Oct/2016 16:33:56] "POST /publish/ HTTP/1.1" 200 591
ee
所以看起来即使使用post,使用正确的modelForm仍然不会调用form_valid或form_invalid方法。即使是post方法也不会被调用。
有人知道发生了什么事吗?
答案 0 :(得分:0)
post方法和get方法的超级调用应包括* args和** kwargs:
return super(Publish, self).post(request,*args,**kwargs)
这将传递包括表格数据在内的其他信息。
答案 1 :(得分:0)
实际上,我创建的中间件导致了这种情况。 感谢。