python - Django 模板将 HTML 参数渲染为文本

标签 python html django

我创建了一个需要来自 Django View 的参数的 Django 模板。其中一个参数是一些显示甘特图的 html。它不是将参数呈现为正确的 html,而是将其呈现为文本。

为什么将参数内容视为文本而不是识别 <script><div>标签?

我传递给 Django View 的参数中的 html 如下所示:

        <script language="Javascript" type="text/javascript">
           window.onload = function() {
           var MyVar = [
            {label: "Label-less", times:[{"starting_time":1420228800000,"ending_time":1420257600000}, {"starting_time":1420264800000, "ending_time":1420293600000}, ]},
            ];

           function drawtimeline_MyVar() {
              var chart = d3.timeline()
              .showBorder()
              .stack() // toggles graph stacking
              ...
              *(code removed here for size purposes)*
              ...
             var svg = d3.select("#DIV_MyVar").append("svg").attr("width", 1000)
             .datum(MyVar).call(chart);
              }


        //Call the function
        drawtimeline_MyVar();
        }

       </script>

       <div>
           <h3>Mehe</h3>
           <div id="DIV_MyVar"></div>
       </div>
       <div id="DIV_MyVar_hover" class="hoverDiv">
           <div class="coloredDiv"></div>
              <table border=1 cellpadding=10>
                  <tr>
                    <td>
                      <p>Task ID: </p><div id="name"></div>
                    </td>
                  </tr>
              </table>

       </div>

参数 comm_request 在模板底部使用,如下所示:

<html>

  <head>
    <title>Search Results</title>
    <meta charset="utf-8">
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <script language="Javascript" type="text/javascript" src="./Review/d3-timeline-master/src/d3-timeline.js"></script>
    <script type="text/javascript">
    {{ JSLIB }}
    </script>
    <STYLE type="text/css">
        .axis path,
        .axis line {
          fill: none;
          stroke: black;
          shape-rendering: crispEdges;
        }
        .axis text {
          font-family: sans-serif;
          font-size: 10px;
    </STYLE>

</head>

<body>
<div class="container">
    <div class="page-header">
        <h1>
            <p class="text-center">Much Results</p>
        </h1>
    </div>
</div>
<div class="container">
    <h3><p class="text-center">Charts</p></h3>
</div>
<div class="container">
    <h4 class="text-center">
        Gantt Chart
    </h4>
</div>
{{ comm_requests }}
</body>
</html>    

最佳答案

在模板中,您可以使用safe 过滤器。

{{ variable_with_html_tag_value | safe }}

关于python - Django 模板将 HTML 参数渲染为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515767/

相关文章:

python - '@reify' 有什么作用,什么时候应该使用它?

html - 与 HTML 中的方法 POST 和 GET 相关的问题

python - 创建从 django 模型中的另一个 ImageField 派生的 django ImageField

python - PlaySound() 减慢进程

python - 如何将范围集合减少到最小范围集

python - 从边框提取轮廓

html - 如何使CSS子菜单链接比父链接长

javascript - 为什么我的 HTML 构造函数对象返回 [object Object] 而不是 [HTMLElementElement]?

django - raise ImportError("无法导入设置,因为环境变量 %s 未定义

ruby-on-rails - rails : How to generate translation files like django