首页 > 解决方案 > 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格式的。将变量“转换”为字符串然后比较两个值的正确方法是什么?

标签: pythonhtmldjangojinja2

解决方案


您可以使用字符串格式模板标签https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#stringformat

{% if msg.sent_by_id == request.user.public_id|stringformat:"s" %}

推荐阅读