我目前正在使用django的CreateView将数据发布到数据库。不幸的是,我从不调用我想保存并执行自定义逻辑的方法form_valid()。我读到另一个堆栈溢出响应,form_invalid()可能会发现问题,但我无法调用该方法。似乎调用的唯一方法是get(),我将其覆盖并放入一个print语句。我做错了什么?
view.py中的类声明
class TeamCreate(CreateView): # Manipulate and use this Method instead of create_team
model = Team
# form_class = create_team_form
fields = ['team_name', 'sport', 'sport_season']
success_url = '/'
def get(self, request, *args, **kwargs):
self.user = request.user
print 'happening1'
return super(TeamCreate, self).get(request, *args, **kwargs)
def form_valid(self, form):
print 'happening2'
# form.instance.save()
# self.user.teams.add(form.instance)
form.save()
return super(form_valid, self).form_valid(form)
def form_invalid(self, form):
print "form is invalid"
return http.HttpResponse("form is invalid.. this is just an HttpResponse object")
相关模板代码
{% block content %}
<form action="" method=”POST”>
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary" type="submit" value="Create" />
</form>
答案 0 :(得分:25)
显然,POST周围的引号根本不是引号,而是鬼鬼祟祟的忍者生活破坏痕迹。 “对比。”我要睡觉了。
答案 1 :(得分:3)
试试这个
<form action="" method="post">