在 Django 模板中使用 JSON [英] Using JSON in django template

查看:25
本文介绍了在 Django 模板中使用 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 JSON 的变量,我需要传递给模板.我将它定义为一个变量,然后成功地将它传递到模板中.但是,我需要将引号替换为 " 的格式,但正在替换为 '.这会导致我将其传递给的服务出现问题.

I have a variable that contains JSON I need to pass into a template. I'm defining it as a variable and then passing it into the template successfully. However, I need the format to replace the quotes with ", but is replacing with '. This is causing issues with the service that I"m passing this to.

image_upload_params = 
{
  "auth": {
    "key": "xxx"
  },
  "template_id": "xxx",
  "redirect_url": "url-here",
}

这是模板中出现的方式:

Here is how it's coming up in the template:

{'redirect_url': 'url-here', 'template_id': 'xxx', 'auth': {'key': 'xxx'}}

知道如何使用它吗?取而代之?

Any idea how to get it to use " instead?

推荐答案

使用 SafeString:

from django.utils.safestring import SafeString

def view(request):
    ...
    return render(request, 'template.html', {'upload_params': SafeString(json_string)})

这篇关于在 Django 模板中使用 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆