首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TemplateSyntaxError at /==comm.user.id/10/无法解析其余部分:“request.user.id==comm.user.id”中的“==comm.user.id”

TemplateSyntaxError at /==comm.user.id/10/无法解析其余部分:“request.user.id==comm.user.id”中的“==comm.user.id”
EN

Stack Overflow用户
提问于 2022-06-29 09:42:49
回答 2查看 38关注 0票数 0

在使用for循环之后,我会得到一个特定的注释及其相关用户,并且只希望删除该用户编写的注释。如何在{% if -%}中写入变量。

代码语言:javascript
复制
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 %}
EN

回答 2

Stack Overflow用户

发布于 2022-06-29 10:07:11

使用变量的唯一方法是使用with标记。示例:

代码语言:javascript
复制
{% with name="World" %}     
<html>
<div>Hello {{name}}!</div>
</html>
{% endwith %}

然而,在您的情况下,这可能是不可能的。我建议在作为请求的一部分发送的注释上设置一个自定义属性,这样您就可以使用comm.can_delete来检查用户是否可以删除注释。将can_delete属性添加到后端,根据需要将其值设置为request.user.id == comm.user.id

我不知道你的后端是什么样子,所以我不能告诉你怎么做,但希望你能理解我的观点。

票数 1
EN

Stack Overflow用户

发布于 2022-06-29 10:07:21

也许你错过了一些空位?

代码语言:javascript
复制
{% if request.user.id == comm.user.id %}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72799196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档