问题描述
我是 Flask 的新手,并尝试从 GeeksForGeeks 运行这个简单的代码
form.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>
flask_text.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 但没有错误日志
解决方法
首先,你的代码缩进是错误的。试试这个:
# 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安装烧瓶
如果它仍然不起作用,请给我更多的上下文。