Django:在 Django 模板引擎中将 uuid 值转换为字符串

Django: Converting uuid value to a string in django template engine

我有这个行代码,我需要启动 运行。

<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格式。 'converting' 一个变量到一个字符串然后比较两个值的正确方法是什么?

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

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