python - Django:在 Django 模板引擎中将 uuid 值转换为字符串
问题描述
我有这段代码需要启动并运行。
<h4 class="page-header">
{% if msg.sent_by_id == request.user.public_id|string %}
{% if request.user.role == 'administrator' %}Admin Replied
{% elif request.user.role == 'user' %}Your reply
{% endif %}
{% endif %}
</h4>
问题是这两个值是相同的,但是它们都是其他格式。msg.sent_by_id
是一个字符串,也不是request.user.public_id
,这个值是uuid
格式的。将变量“转换”为字符串然后比较两个值的正确方法是什么?
解决方案
您可以使用字符串格式模板标签https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#stringformat
{% if msg.sent_by_id == request.user.public_id|stringformat:"s" %}
推荐阅读
- android - Android Paging 3 With RemoteMediator,无法添加页眉或页脚
- vue.js - VueJS 根据条件隐藏和显示元素
- javascript - 如何在 react-native 中获取第二行文本响应
- jasper-reports - 在页面底部渲染细节带在另一个细节带之前
- java - 在 wicket 中自定义 ModalDialog 组件
- vba - 过滤后的数据副本正在保存到新工作簿中的工作表 2
- python - 如何在基于类的密码重置视图中使用占位符?
- flutter - 开发服务器上的 Flutter 驱动程序失败(无法连接到 Chrome 调试端口:37475)
- reactjs - Invariant Violation: The navigation prop is missing for this navigator. In react-navigation v3 and v4 you must set up your app container directly
- spring-boot - 如何在 SQS 通信中使用记录 Sleuth Trace ID 来配置 JMS 侦听器