【问题标题】:Django: Converting uuid value to a string in django template engineDjango:在 Django 模板引擎中将 uuid 值转换为字符串
【发布时间】:2020-11-20 17:18:09
【问题描述】:

我有这段代码需要启动并运行。

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

【问题讨论】:

  • str (uuid) 在您的视图中并将 str 版本传递到您的模板中
  • 我正在传递整个 json 对象 -return render(request, 'service_desk/ticket_status.html', {'ticket': Ticket_obj, 'ticket_messages': Ticket_obj_messages})

标签: python html django jinja2


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-07-23
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2013-07-16
    • 1970-01-01
    相关资源
    最近更新 更多