我有一个应该处理表单提交的视图。模板中的 HTML 表单应该发送帖子,但视图只接收 GET 请求。
看法:
def eventSell(request, id):
event = Event.objects.get(pk = id)
if request.user.is_authenticated():
print request.user
if request.method == ['POST']:
print 'post'
form = ListingForm(request.POST)
if form.is_valid():
print 'form is valid'
user = request.user
price = request.POST['price']
t = Object(event = event, price = price, seller = user, date_listed = timezone.now())
t.save()
return HttpResponseRedirect(reverse('app:index'))
else:
print 'get'
form = ListingForm()
return render_to_response('app/list.html', {'form' : form, 'event' : event}, context_instance = RequestContext(request))
else:
return HttpResponseRedirect(reverse('allauth.accounts.views.login'))
模板:
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
我真的很难过,所以任何建议都会非常感激。谢谢。