【问题标题】:Flask project with HTML is not working [duplicate]带有 HTML 的 Flask 项目不工作 [重复]
【发布时间】:2017-06-12 10:14:15
【问题描述】:

我正在尝试运行这个 Flask 项目,但它不起作用。有谁知道为什么?

.py 文件:

from flask import Flask, render_template

app = Flask(__name__)


@app.route("/")
def hoofdpagina():
    return render_template("afvink2.html")


if __name__ == '__main__':
    app.run()

HTML 文件:

<!DOCTYPE html>
<html>
<body onunload="Reset()"  style="background-color:Pink;">
<head>
                <title>Messenger </title>
        </head>

        <h3>Messenger</h3>
Messagebox:<br> <textarea id="chatbox" cols="50" rows="5"></textarea> <br><br>
<br><input type="text" id="P1" value="ADI" ><input type="text" id="first"><button onclick="B1Function()">Send</button><br><br>
<br><input type="text" id="P2" value="JS" > <input type="text" id="second"><button onclick="B2Function()">Send</button>


<script>
function B1Function() {
    document.getElementById("chatbox").value += document.getElementById("P1").value ;
    document.getElementById("chatbox").value += ": " ;
    document.getElementById("chatbox").value += document.getElementById("first").value ;
    document.getElementById("chatbox").value += "\r"
    document.getElementById("first").value = ""

}
function B2Function() {

    document.getElementById("chatbox").value += document.getElementById("P2").value ;
    document.getElementById("chatbox").value += ": " ;
    document.getElementById("chatbox").value += document.getElementById("second").value ;
    document.getElementById("chatbox").value += "\r"
    document.getElementById("second").value = ""

}
function Reset() {
    document.getElementById("Berichtenbox").value = ""
    document.getElementById("first").value = ""
    document.getElementById("second").value = ""

}
</script>

</body>
</html>

错误:

C:\...\...\...\
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [12/Jun/2017 12:12:07] "GET / HTTP/1.1" 500 -

页面http://127.0.0.1:5000/ 说:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

有谁知道我该如何解决这个问题?谢谢!

【问题讨论】:

  • 这是您唯一的代码吗?我可以毫无问题地在本地运行它
  • 您可以尝试使用app.run(debug=True) 让服务器显示调试器,以防发生异常
  • 你能不能把你的app.run()改成app.run(debug=True)并重新发布错误信息

标签: python html python-3.x flask localhost


【解决方案1】:

你的项目结构应该是这样的。

├── app.py
└── templates
    └── afvink2.html

render_template 方法默认查看 templates 目录。

您也可以将模板目录配置为viewspublic

通过简单的提一下flask中的参数

app = Flask(__name__, template_folder="views")
app = Flask(__name__, template_folder="path/to/whatever")

请检查烧瓶提供的配置。 他们的documentation 非常整洁。

【讨论】:

  • 谢谢!!工作:)
  • 很高兴知道 :)
【解决方案2】:

Flask 会在“templates”目录中查找模板。将您的 html 模板移动到模板目录中。它应该工作

【讨论】:

  • 谢谢! :) 工作!
猜你喜欢
  • 2016-09-04
  • 2018-06-27
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2015-01-08
  • 2018-12-29
  • 1970-01-01
  • 2021-09-22
相关资源
最近更新 更多