【问题标题】:How to render multiple files in Flask如何在 Flask 中渲染多个文件
【发布时间】:2020-06-05 09:39:41
【问题描述】:

我正在尝试基于 python 烧瓶制作自己的网站,内容将是关于空间和使用 Nasa 的 Api 我有三个文件 index.html、nasa.js、starfield.js 我必须将所有这些文件渲染到我的烧瓶程序 我已经尝试过互联网上的所有方法 你能告诉我该怎么做吗 所有代码都在下面的链接中: index.html - https://pastebin.com/miVx2FZb index.html nasa.js - https://pastebin.com/HfJqYRNj nasa.js starfield.js - https://pastebin.com/7CdNngU3 starfield.js python代码-https://pastebin.com/0gbxQmu3main.py

任何建议将不胜感激, 提前致谢, 问候, 查兰纳帕

【问题讨论】:

  • 您可能需要阅读How to Ask 并相应地编辑您的问题。

标签: javascript python html


【解决方案1】:

你不能使用flask渲染JS文件,如果可以的话肯定不推荐。

我也浏览了你的 index.js, 我注意到了两个主要错误:

<script src="nasa.js"></script>
<script src="starfield.js"></script>

在烧瓶应用程序中,您永远不应该这样提供 src。您应该有一个静态文件夹,其中有一个 js 文件夹,您可以在其中存储所有 js 文件。

您的&lt;script&gt; 应如下所示:

<script src="url_for('static', filename='js/nasa.js')">
<script src="url_for('static', filename='js/starfield.js')">

在你的python文件中你应该有一个render_template

from flask import Flask, render_template
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, threaded=True)

确保你有一个名为 templates 的文件夹,其中有你的 index.html 和你的静态文件夹。

【讨论】:

  • 我尝试渲染文件,但它根本不起作用你有没有其他方法谢谢你帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多