我想允许用户编辑自己的评论。我试图比较comment.nick以及request.user.username和request.user.get_username。例如,comment.nick返回“作者”,request.user.username将返回“作者”,但“编辑”将不可见。
{% if request.user.username == comment.nick %}
<a href="{% url 'edit_comment' comment.id %}">Edit</a>
{% endif %}
# models.py
class Commentary(models.Model):
nick = models.ForeignKey('Profile', on_delete=models.CASCADE, max_length=20)
comment = models.TextField(max_length=300)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
added = models.DateTimeField(auto_now_add=True, blank=True)