flask - Flask 内部服务器错误但没有错误日志
问题描述
我是烧瓶的新手,并试图从 GeeksForGeeks 运行这个简单的代码
表单.html
<form action="{{ url_for("gfg")}}" method="post">
<label for="firstname">First Name:</label>
<input type="text" id="firstname" name="fname" placeholder="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lname" placeholder="lastname">
<button type="submit">Login</button>
烧瓶文本.py
# importing Flask and other modules
from flask import Flask, request, render_template
# Flask constructor
app = Flask(__name__)
# A decorator used to tell the application
# which URL is associated function
@app.route('/', methods =["GET", "POST"])
def gfg():
if request.method == "POST":
# getting input with name = fname in HTML form
first_name = request.form.get("fname")
# getting input with name = lname in HTML form
last_name = request.form.get("lname")
return "Your name is "+first_name + last_name
return render_template("form.html")
if __name__=='__main__':
app.run()
https://www.geeksforgeeks.org/retrieving-html-from-data-using-flask/
您可以在上面的链接中找到代码我添加了 app.debug = True 但没有错误日志
我在 /templates 文件夹中有我的 html 文件
解决方案
首先,您的代码缩进是错误的。尝试这个:
# importing Flask and other modules
from flask import Flask, request, render_template
# Flask constructor
app = Flask(__name__)
# A decorator used to tell the application
# which URL is associated function
@app.route('/', methods =["GET", "POST"])
def gfg():
if request.method == "POST":
# getting input with name = fname in HTML form
first_name = request.form.get("fname")
# getting input with name = lname in HTML form
last_name = request.form.get("lname")
return "Your name is "+first_name + last_name
return render_template("form.html")
if __name__=='__main__':
app.run()
第二,你的虚拟环境在运行吗?(你能在命令行的开头看到 '(env)' 吗?)如果没有,你需要先执行:
source env/bin/activate
然后 pip3 安装烧瓶
如果它仍然不起作用,请给我更多背景信息。
推荐阅读
- vim - 如何重复x,n次?
- javascript - 使用 javascript 分块对象数组
- python - 使用另一个函数中的变量的正确方法是什么?
- mql4 - 为什么 mql4 中的 CryptEncode() 与其他语言不同?
- javascript - 在没有元掩码的情况下自动进行solidity传输
- vue.js - 当使用索引导航选项卡时,Bootstrap vue Tab 组件不起作用
- ddev - 如何使用 DDEV 升级到 Drupal 9
- ios - Flutter 在运行 build 或 run 时抛出错误
- ocaml - 没有标签就不能应用 Ocaml 参数
- django - 无法将关键字“用户名”解析为字段。选项有:地址、年龄、城市、国家、性别、姓名、电话、pt_id、用户、user_id