Django 无法使用模板标记过滤器设置ace编辑器的值

Django 无法使用模板标记过滤器设置ace编辑器的值,django,django-templates,ace-editor,Django,Django Templates,Ace Editor,我的html页面中有一个ace编辑器,我可以为它编写代码 editor.setValue( '//Your code here'); 它完美地工作,我得到一个编辑器,里面写着“//你的代码在这里”。 但是当我从django后端传递一个模板标记时,使用这行代码 def func(request): context={"message":"hello there,write your code"} return render(request,'index.html',c

我的html页面中有一个ace编辑器,我可以为它编写代码

editor.setValue( '//Your code here');
它完美地工作,我得到一个编辑器,里面写着“//你的代码在这里”。 但是当我从django后端传递一个模板标记时,使用这行代码

def func(request):
       context={"message":"hello there,write your code"}
       return render(request,'index.html',context)
并尝试使用此行的模板标记筛选器设置值

editor.setValue({{message}});

它不起作用,如果我用简单的html编写它,它就起作用了,但在setValue()函数中不起作用。

这不起作用的原因是消息不会用引号括起来

您应该对传递给模板的字符串进行JSON转储:

from json import dumps as jdumps

def func(request):
    context={'message': jdumps('hello there,write your code')}
    return render(request,'index.html',context)

字符串没有用引号括起来。你解决了我大量的工作,我正在考虑更改models.py等。真的非常感谢:)我今天找到了另一个解决方案,context={'message':'this is a message'},在html页面中使用:{{{message | escapejs}”
editor.setValue({{ message|safe }});