无法获得表单评论帖子

时间:2017-04-04 15:16:17

标签: python django

好的,这将是我的最后一次尝试,为什么我不能让FormComment评论detail.html中的帖子?我对此感到紧张......

def DetailView(request, pk):
    if request.user.is_authenticated():
        base_template_name = 'blog/base.html'
    else:
        base_template_name = 'blog/visitor.html'
    post = get_object_or_404(Post, pk=pk)
    form = CommentForm(request.POST or None)
    if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('blog:DetailView')
    return render(request,'blog/detail.html',{'post':post,'from':form ,'base_template_name':base_template_name})

form.py

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('textc',)

这里是detail.html(部分内容)

{% if post.comment_set.all %}
                <h2>Comments</h2>
            <div class="comments">
            {% for comment in post.comment_set.all %}
                <span>
                <a href="">{{ comment.user }}</a> said on {{ comment.created_on }}
                    </span>
                <p>
            {{ comment.textc }}
                 </p>
                {% endfor %}
</div>
{% endif %}
                    <h4>Leave a Comment:</h4>
                    <form action="" method="POST">
                {% csrf_token %}
                <table>
                        {{ form.as_p }}
                </table>
                <input type="submit" name="submit" value="Submit" />
                </form>
                </div>

CommentModel:

class Comment(models.Model):
    post = models.ForeignKey('blog.Post', null=True,related_name='comments')
    user = models.ForeignKey(User, max_length=200, blank=True, null=True)
    textc = models.TextField(null=True,max_length=200)
    created_date = models.DateTimeField(default=timezone.now)

1 个答案:

答案 0 :(得分:2)

你在视图的最后一行有一个拼写错误;您在模板上下文中有form而不是if indexPath.row == 4 || indexPath.row == 9 || indexPath.row == 14 { count+=1 //which is initially set to zero }