django - 如何将我的简单自定义 django 模板标签与 if 语句一起使用?
问题描述
我正在尝试在 django 模板中使用我的简单标签。据我所知,赋值标签已从 Django 2.0 中删除。我的模板标签是:-
@register.simple_tag
def channelpostlike(postid,userid):
print(userid)
postresult=ChannelPost.objects.get(id=postid)
if postresult.user_like.filter(id=userid).exists():
return True
else:
return False
我想像这样使用它:-
{%if channelpostlike c.id request.user.id %}
解决方案
文件:
assignment_tag 自 1.9 版以来已弃用 simple_tag 现在可以将结果存储在模板变量中,应改为使用。
所以你可以simple_tag
这样使用:
# You can access the result as something anywhere you like
{% channelpostlike c.id request.user.id as something %}
{% if something %}
<p>Something ...</p>
{% endif %}
推荐阅读
- javascript - 如何将图标添加到 chrome 扩展?
- reactive-programming - 反应式单声道如何将订阅上下文传播到`doOnSubscribe`和`doFinally`
- jquery - jQuery 和 CSS:侧边栏应该在 flexbox 内容之上推出,而不影响主要区域
- c++ - 摆脱“未声明的标识符”错误
- html - 为什么我的 HTML 表单显示奇怪的符号?
- c# - C#程序返回通过Active Directory连接的开机机器
- python - 在python中添加列表的意外结果
- javascript - 为什么我们在 javascript 中的 .split 函数之前放置 ''
- javascript - 如何检测用户是否滑动浏览器后退导航
- vue.js - Nuxt 2.12.2:使用新的 fetch 方法填充存储