注释系统未存储在我的数据库中

时间:2018-11-20 02:43:49

标签: django django-models

每次尝试发表评论时,都不会出现错误,但是我的评论似乎没有存储在我的评论数据库中,但是在数据库中已存储评论的列表下方,该计数很重要,但不会显示在数据库中,其次,在我的模板中,我有:

{% with comments.count as total_comments %}
    <h2>{{ total_comments }} comment{{ total_comments|pluralize }}</h2>
{% endwith %}

如果我的表单有效并已存储,我想自动计算我的评论列表,这是我的一段代码,可以更好地理解...。

views.py:

def SecondDetail(request,seconds): 
    seconds =get_object_or_404(Second,id=seconds)
    comments = seconds.comments.filter(approved=True) 
    if request.method == 'POST':
        form = CommentForm(data=request.POST)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.seconds = seconds
            new_comment.save()
            return HttpResponse("thanks for commenting")

    else:
        form = CommentForm()
    return render (request, 'home/second_detail.html', {'form':form,'comments':comments,'seconds':seconds})

我的url.py

url(r'^news/(?P<seconds>\d+)/$', views.SecondDetail,name='post_detail')

和我的second_detail.html(模板)

{% with comments.count as total_comments %}
<h2>{{ total_comments }} comment{{ total_comments|pluralize }}</h2>
{% endwith %}
{% for comment in comments %}
<div class="comment">
<p class="info">
Comment {{ forloop.counter }} by {{ comment.name }}
{{ comment.created }}
</p>
{{ comment.body|linebreaks }}
</div>
{% empty %}
<p>There are no comments yet.</p>
{% endfor %}

{% if new_comment %}
<h2>Your comment has been added.</h2>
{% else %}
<h2>Add a new comment</h2>
<form action="." method="post">
{{ form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Add comment"></p>
</form>
{% endif %}

forms.py

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

谢谢

0 个答案:

没有答案