我试图为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}),
}
谢谢你的帮助!你知道吗
表单标记中需要一个
action
属性。既然你正在发布到同一个网址,它应该是相关问题 更多 >
编程相关推荐