[英]Form not submitting information (Django)
我正在尝试为django博客项目创建评论,但是单击表单提交按钮时没有任何反应。
这是模板的html。
<form role="form" method="post">
<div class="input-group">
{% csrf_token %}
{% for field in form %}
{{ field }}
{% endfor %}
<p>Comment: </p>
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Submit</button>
</span>
</div>
</form>
这是我按下按钮时试图调用的视图。
def detail(request, slug):
context ={}
post = BlogPost.objects.get(slug=slug)
# print(request.method)
if request.method=='POST':
form = CommentForm(request.POST)
else:
form = CommentForm()
if form.is_valid():
t = form.save(commit=False)
t.commentTime = datetime.datetime.now()
t.save()
return HttpResponseRedirect(reverse('blogpost_detail'))
comment_list=Comments.objects.order_by('-commentTime')[:25]
context = {'comment':comment_list,'form':form, 'post': post}
return render(request, 'blog/blogpost_detail.html', context)
这是模板中调用的表单。
class CommentForm(forms.ModelForm):
class Meta:
model = Comments
fields=('commentText', 'commentImage',)
exclude =('post','commentTime',)
widgets={
'commentText': forms.Textarea(attrs={'col':10}),
}
谢谢您的帮助!
您需要在form标记中添加一个action
属性。 由于您要发布到相同的URL,因此它应该是
<form action="." method="post " role°"form">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.