comments 被注册在所需的页面上,对于没有登录的人,如果他们想要留下 comments ,它会先进入网站,但后来 comments 是以匿名用户的名字注册的.我不想让这个登记发生. 应该编辑哪些部分以及如何编辑?
在views.py:
comments = Comment.objects.filter(product=product)
if request.method == 'POST':
# comment
if 'comment' in request.POST:
author = request.user
content = request.POST.get('content')
comment = Comment(product=product, author=author, content=content)
comment.save()
context = {
'comments': comments,
}
return render(request, 'auctions/product_detail.html', context)
在product_Detail.html中:
<h3 id="h3">Comments</h3>
{% if user.is_authenticated %}
<ul>
{% for comment in comments %}
<li><a>{{ comment.author }} : {{comment.content}}</a></li>
{% endfor %}
</ul>
{% else %}
Not signed in.
{% endif %}
说
预先感谢您的帮助