首页 > 解决方案 > 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 文件

标签: flask

解决方案


首先,您的代码缩进是错误的。尝试这个:

# 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 安装烧瓶

如果它仍然不起作用,请给我更多背景信息。


推荐阅读