【问题标题】:Codemirror editor set multiple line valueCodemirror 编辑器设置多行值
【发布时间】:2017-01-02 11:34:14
【问题描述】:

这是创建 codemirror 对象的代码

var myCodeMirror = CodeMirror(document.getElementById('text_area'), {
  mode:  "text/x-c++src",
  lineNumbers: true,
  indentUnit: 4,
});

提交代码后,我得到了代码,但我也希望它在发布后保留在代码编辑器中, 当代码是多行时,这不起作用

myCodeMirror.setValue("{{submitted_code}}")

codemirror 编辑器如何使用多行?

编辑:

这里是必要的模板代码

<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>

【问题讨论】:

  • 什么是{{submitted_code}}??您的代码中有很多事情无法解释。myCodeMirror.getValue() 是什么?
  • code_editor div 中的代码被复制到 user_code。然后我从 post 方法中获取代码,然后再次渲染同一页面,该页面应保留之前的代码,该代码在 submit_code 中

标签: python django codemirror


【解决方案1】:

可以这样做:

var html = `{{submitted_code}}`;

editor.setValue(html);

使用多行字符串:``

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多