python - Flask render_template 不渲染任何内容

标签 python html flask

问候

我有一个 flask 应用程序:

    @app.route('/test')
    def test():
        return render_template('index.html')

在index.html中我有:

    <!DOCTYPE html>
    <meta charset="utf-8">
    <style type="text/css">

        .node {
        cursor: pointer;
      }

      .overlay{
          background-color: #e6d1ee;
      }

      .node circle {
        fill: #fff;
        stroke: #4d4bb4;
        stroke-width: 2px;
      }

      .node text {
        font-size:10px;
        font-family:sans-serif;
      }

      .link {
        fill: none;
        stroke: #7acc7e;
        stroke-width: 2px;
      }

      .templink {
        fill: none;
        stroke: #ff8285;
        stroke-width: 3px;
      }

      .ghostCircle.show{
          display:block;
      }

      .ghostCircle, .activeDrag .ghostCircle{
           display: none;
      }

    </style>
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="MyTree.js"></script>
    <body>
        <div id="tree-container"></div>
    </body>

当我在浏览器中运行此 html 代码时,它显示了一个很好的 graph

但是后来我使用带有 render_template 的 Flask 来渲染它,它绝对显示了

如何修复它?

抱歉这个巨大的代码块,我不确定这是 html 问题

更新:

实际上是的,问题出在静态的东西上。现在我的浏览器可以获取js文件了。但另一个问题又出现了。在 MyTree.js 中我使用

    treeJSON = d3.json("{{ url_for('static', filename='dota.json') }}", function(error, treeData)

并且 dota.json 未在浏览器中加载。但它与 MyTree.js 位于同一目录中,位于静态目录中。

最佳答案

我想说的是,目前浏览器尚未获取您的 MyTree.js 文件(您可以在开发人员控制台中检查这一点)。您需要将 MyTree.js 文件放置在静态目录中,并在 HTML 中引用静态端点:

<script src="{{ url_for('static', filename='MyTree.js') }}"></script>

关于python - Flask render_template 不渲染任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43632838/

相关文章:

python - 安装包 Beautiful Soup 失败。错误消息是 "SyntaxError: Missing parentheses in call to ' print'"

python - Flask-Sqlalchemy 为 'unique' 整数取非唯一值

python - 如何使用flask-ReSTplus记录桩身?

python - 如何在 Docker 容器中运行 Python Flask

python - unittest 的 setUpClass 类方法能否返回一个值以在其他测试中使用?

python - 使用 selenium 和 python 选择下拉菜单时遇到问题

python - 在 Python3.7 之前的版本中,应该如何在 argparse-module 中处理散布/混合参数?

具有不同宽度边框的html表格

java - 如何将隐藏字段值从html页面传递到GWT页面?不同应用程序中的两个页面

javascript - 当 JS 变量等于 x 时,尝试在 HTML div 选项卡中显示某些 "storyline"