表格未提交信息(Django)

2024-10-03 06:18:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为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}),
       }

谢谢你的帮助!你知道吗


Tags: form模板表单requestcontextcommentpost按钮