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

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

问题描述

我有一个包含JSON的变量,我需要传入一个模板。我将其定义为一个变量,然后将其成功传递给模板。但是,我需要使用替换引号的格式,而是用'替换。这导致了我传递给我的服务问题。

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

以下是模板中的显示方式:

  {' redirect_url&#39 ;:' url-here'' template_id&#39 ;;' xxx'' auth&#39 ;: {' key&#39 ;:' xxx'}} 

任何想法如何使用?

解决方案

使用 SafeString

 c $ c> from django.utils.safestring import SafeString 

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


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?

解决方案

Use 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天全站免登陆