Python 代码镜像编辑器设置多行值
下面是创建codemirror对象的代码Python 代码镜像编辑器设置多行值,python,django,codemirror,Python,Django,Codemirror,下面是创建codemirror对象的代码 var myCodeMirror = CodeMirror(document.getElementById('text_area'), { mode: "text/x-c++src", lineNumbers: true, indentUnit: 4, }); 在我提交代码后,我得到了代码,但我也希望它在发布后保留在代码编辑器中, 当代码有多行时,这不起作用 myCodeMirror.setValue("{{submitted_code}
var myCodeMirror = CodeMirror(document.getElementById('text_area'), {
mode: "text/x-c++src",
lineNumbers: true,
indentUnit: 4,
});
在我提交代码后,我得到了代码,但我也希望它在发布后保留在代码编辑器中,
当代码有多行时,这不起作用
myCodeMirror.setValue("{{submitted_code}}")
如何使用多行代码镜像编辑器
编辑:
这里是必要的模板代码
<script>
function codeGetter()
{
document.getElementById("user_code").value = myCodeMirror.getValue()
}
</script>
{% load static %}
<script src="{% static 'codemirror.js' %}"></script>
<link rel="stylesheet" href="{% static 'codemirror.css' %}">
<script src="{% static 'clike.js' %}"></script>
<form action = "{% url 'round2:handle_answer' user_id selected_question.pk %}" method = "post" onclick=codeGetter()>
{% csrf_token %}
<div id="code_editor" name = "code_editor" style="margin-bottom: 0px; font-weight: bold;">Code Editor</div>
<div id="text_area" name="text_area" style="margin-top: 0px;" ></div>
<input type="submit" name="submit" value="submit" id="submit" style="margin-left:75%" >
<textarea id="user_code" name="user_code"></textarea>
</form>
函数codegatter()
{
document.getElementById(“用户代码”).value=mycodError.getValue()
}
{%load static%}
{%csrf_令牌%}
代码编辑器
可以按如下方式执行:
var html = `{{submitted_code}}`;
editor.setValue(html);
使用多行字符串:`什么是{{submitted\u code}}??您的代码中有很多东西是无法解释的。什么是MyDeMirror.getValue()?代码编辑器div中的代码被复制到用户代码中。然后我从post方法中获取代码,然后再次呈现相同的页面,该页面应保留提交的代码中的先前代码