在使用for循环之后,我会得到一个特定的注释及其相关用户,并且只希望删除该用户编写的注释。如何在{% if -%}中写入变量。
Template error:
In template C:\Users\SHAFQUET NAGHMI\socialnetwork\socialapp\templates\socialapp\comment.html, error at line 27
Could not parse the remainder: '==comm.user.id' from 'request.user.id==comm.user.id'
17 : <P>Please <a href="{% url 'login' %}">login</a> to add comment </P>
18 :
19 : {% endif %}
20 : </form>
21 : <h3>comments..</h3>
22 :
23 : {% for comm in comments %}
24 :
25 : <a class="comment-user" href="{% url 'profile' comm.user.username %}">{{comm.user}}</a>
26 : {{comm.comment}}
27 : {% if request.user.id==comm.user.id %}
28 : <a class="delete" href="/delete_comment/{{post.id}}/{{comm.id}}/">Delete</a>
29 : {% endif %}
30 : <br><br>
31 : {% endfor %}
32 : <!--{{post}} {{comm.id}}-->
33 :
34 : </div>
35 : </div>
36 : {% endblock %}发布于 2022-06-29 10:07:11
使用变量的唯一方法是使用with标记。示例:
{% with name="World" %}
<html>
<div>Hello {{name}}!</div>
</html>
{% endwith %}然而,在您的情况下,这可能是不可能的。我建议在作为请求的一部分发送的注释上设置一个自定义属性,这样您就可以使用comm.can_delete来检查用户是否可以删除注释。将can_delete属性添加到后端,根据需要将其值设置为request.user.id == comm.user.id。
我不知道你的后端是什么样子,所以我不能告诉你怎么做,但希望你能理解我的观点。
发布于 2022-06-29 10:07:21
也许你错过了一些空位?
{% if request.user.id == comm.user.id %}https://stackoverflow.com/questions/72799196
复制相似问题