【问题标题】:Basic flask form handling基本的烧瓶形式处理
【发布时间】:2018-04-18 12:56:53
【问题描述】:

我正在尝试在 Flask 中创建一个基本表单,该表单将接收输入,对其进行操作,然后返回输出。我遇到的问题是,当我运行终端并尝试使我的应用程序在 http://127.0.0.1:5000/ 服务器上运行时,文件不可见。不确定错误在哪里?

这就是我整理文件的方式:

/Users/eas/Desktop/grota/templates/index.html

/Users/eas/Desktop/grota/templates/age.html

/Users/eas/Desktop/grota/app.py

这是 app.py 文件

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

@app.route('/send',methods = ['GET','POST'])
def send():
    if request.method == 'POST':
        age = request.form['age']
        return render_template('age.html',age=age)
    return render_template('index.html')
if __name__ == '__main__':
app.run()

这是 index.html 文件

<!DOCTYPE html>
<html>
  <head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
  </head>

  <body>
     <h1>How old are you?</h1>
    <form method="POST" action = "/send">
      <div class = "form-group">
        <input type="text" name = "age">
      </div>
      <input class="btn btn-primary" type="submit" value="submit">
    </form>

  </body>

</html>

这是age.html文件

<!DOCTYPE html>
<html>
  <head>
  </head>

  <body>
      <h1>Your age is{{age}}</h1>
  </body>

【问题讨论】:

    标签: python flask


    【解决方案1】:

    你没有设置根路由。

    或者你打开http://127.0.0.1:5000/send

    或者你可以使用这个快速而肮脏的修复(正如你在装饰器中看到的那样,现在 / 和 /send 都被考虑在内了):

    from flask import Flask, render_template,request
    app = Flask(__name__)
    
    @app.route('/',methods = ['GET'])
    @app.route('/send',methods = ['GET','POST'])
    def send():
        if request.method == 'POST':
            age = request.form['age']
            return render_template('age.html',age=age)
        return render_template('index.html')
    
    if __name__ == '__main__':
        app.run()
    

    如果你不处理'/'路由,当你打开http://127.0.0.1时什么都不会发生

    如果你更清楚地解释你想要获得的结果是什么,我可以提供更好的帮助。

    【讨论】:

    • 我写了后端python脚本,我想在用户输入上运行并将获得的值返回给用户。上面的代码是我想要做的简化版本,它旨在询问用户年龄并将其打印回来。即使您更正了代码,我仍然收到以下错误: Internal Server Error 服务器遇到内部错误,无法完成您的请求。要么服务器超载,要么应用程序出错。
    • 代码在我的电脑上完美运行。这可能是一个简单的缓存问题吗?您是否尝试过删除缓存或使用其他浏览器?
    • 你在 python 终端窗口中看到了什么?
    • TemplateNotFound: index.html 127.0.0.1 - - [18/Apr/2018 20:42:04] "GET /send HTTP/1.1" 500 -
    • 您是否将 index.html 放入模板文件夹(在脚本路径中创建)?
    【解决方案2】:

    试试看这里:http://127.0.0.1:5000/send

    如果这不起作用,您在控制台或浏览器中遇到了什么错误?

    编辑:

    我刚刚试了一下,效果很好。尝试在浏览器的新选项卡中重新加载页面,看看是否仍然出现。此错误与您在网页上输入的命名以及您使用 request.form['age'] 索引表单的方式有关

    【讨论】:

    • Bad Request 我转到127.0.0.1:5000/send 并收到以下错误:浏览器(或代理)发送了此服务器无法理解的请求。
    【解决方案3】:

    在寻找一个我试图解决的问题时,我发现了你的问题。对于包含完整代码(带有 py 和 2 个 HTML 文件)的解决方案,您可以查看我的 repl.it:https://repl.it/@AlperenAtik/NaturalShortActivecell#main.py

    问题是:

    1. 在第一个函数中,您将循环路由到“/send”页面。这就是您的发送页面没有出现的原因。第一个函数的路由需要是“/”
    2. 在索引文件中,源路由显示为“/send”。每当我将其更改为 / 脚本都可以正常工作
    3. 其他评论员已经提到你的触发函数有一个缩进错误。适当添加主机和端口后,您的脚本就可以工作了。

    对于那些喜欢更困难的方式的人——在 stackoverflow 中查看事物——而不是在行动中,我正在添加代码块。享受吧。

    对于 main.py

    from flask import Flask, render_template,request
    app = Flask(__name__)
    
    @app.route('/',methods = ['GET','POST'])
    def send():
        if request.method == 'POST':
            age = request.form['age']
            return render_template('age.html',age=age)
        return render_template('index.html')
    
    if __name__ == '__main__':
        app.run(host = '0.0.0.0', port = 8080
    

    我将主机设置为“0.0.0.0”,端口设置为 8080。如果您在终端上运行 Python,您可以尝试 (host = '127.0.0.1', port:5000')。

    对于模板/index.html

    <!DOCTYPE html>
    <html>
      <head>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
      </head>
    
      <body>
         <h1>How old are you?</h1>
        <form method="POST" action = "/">
          <div class = "form-group">
            <input type="text" name = "age">
          </div>
          <input class="btn btn-primary" type="submit" value="submit">
        </form>
    
      </body>
    
    </html>
    

    对于模板/age.html

    <html>
      <head>
      </head>
    
      <body>
          <h1>Your age is {{age}}</h1>
      </body>
    

    【讨论】:

    • 也许您可以在 stackoverflow 中插入回复以避免插入依赖项。
    猜你喜欢
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    相关资源
    最近更新 更多