python - 如果 comment.name == 用户无法正常工作
问题描述
我希望用户能够删除他们写的评论。不幸的是,虽然我无法让 if 语句起作用。
{% if comment.name == user %}
<a href="{% url 'delete_own_comment' comment.id %}">delete this comment-</a>
{% endif %}
所以我可以看到 user 和 comment.name 的值我包含了下面的代码。这是出于测试目的,不会包含在站点的最终设计中。
<h3 class="article-content">{{ user }}</h3>
<p class="article-content">{{ comment.name }}</p>
我还想测试以确保 if 语句的内部有效。
<a href="{% url 'delete_own_comment' comment.id %}">delete this comment-</a>
{% endif %}
所以我包括了下面的代码。同样,这不会包含在我的最终网站设计中。
{% if post.author == user %}
<a href="{% url 'delete_own_comment' comment.id %}">-------delete this comment---------</a>
{% endif %}
但是通过查看屏幕截图,您可以知道代码无法正常工作。第 30 条评论是“用于 StackOverflow 的测试评论”。该评论由 RossSymonds 撰写。当前登录的用户是 RossSymonds。如果一切正常,您会看到“删除此评论”(与“------删除此评论------”不同)。
有人有建议吗?
{% extends "blog/base.html" %}
{% block content %}
{% load crispy_forms_tags %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="#">{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
{% if post.author == user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' post.id %}">Update</a>
<a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' post.id %}">Delete</a>
</div>
{% endif %}
</div>
<h2 class="article-title">{{ post.title }}</h2>
<p class="article-content">{{ post.content }}</p>
</div>
</article>
<article class="media content-section">
<!-- comments -->
<h3>{{ comments.count }} Comments</h3>
</article>
<!-- comments -->
{% for comment in comments %}
<article class="media content-section">
<div class="media-body">
<small class="text-muted">{{ comment.id}}</small>
<small class="text-muted">{{ comment.name}}</small>
<small class="text-muted">{{ comment.created_on|date:"F d, Y" }}</small>
<div class="media-body ">
<p class="article-content">{{ comment.body }}</p>
<h3 class="article-content">{{ user }}</h3>
<p class="article-content">{{ comment.name }}</p>
</div>
</div>
{% if comment.name == user %}
<a href="{% url 'delete_own_comment' comment.id %}">delete this comment-</a>
{% endif %}
{% if post.author == user %}
<a href="{% url 'delete_own_comment' comment.id %}">-------delete this comment---------</a>
{% endif %}
</article>
{% endfor %}
{% if user.is_authenticated %}
<form action="" method="post">
{% csrf_token %}
{{ comment_form|crispy }}
<input type="submit" value="Submit">
</form>
{% else %}
<article class="media content-section">
Users who are logged in can post comments.
</article>
{% endif %}
{% endblock content %}
解决方案
您是否尝试过替换user
to user.username
in {% if comment.name == user %}
?
{{user}}
包含所有用户的信息(链接到用户模型的每个字段)
例如,如果您想获取用户的用户名,您可以编写{{user.username}}
.
推荐阅读
- python - df.csv 没有保存实际的数据帧
- laravel-8 - 升级到 Passport 10 Lcobucci JWT4.1 后构建 LaravelPassport AccessTokenController 时 Lcobucci\JWT\Parser 不可实例化
- php - Yii XLSX 文件在下载时损坏
- php - 如何在 Laravel8 中进行刀片动态文件夹
- jenkins - 詹金斯管道本地主机:7080
- reactjs - useMemo 渲染道具值有用吗?
- sql - 从具有不同列的另一个表中插入一个表
- r - 删除绘图树状图中的边框
- api - 将与事件相关的参数作为变量附加到对象模型是否是一种好习惯?
- azure-ad-b2c - Azure AD B2C:使用条件访问技术配置文件对有风险的用户进行补救