Python/Flask,在用户单击submit按钮并指向成功页面后,我如何在3秒钟后返回index.html?

2024-05-26 04:22:41 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里呆了一段时间,我回答了所有的问题…直到现在

因此,我正在使用Python/Flask制作我的第一个web应用程序,通过Udemy Python Mega课程,每一条指令都有很多内容,构建一个数据收集器web应用程序

该应用程序本身就是一个婴儿体重猜测应用程序。说实话,两个多月后我就有了我的第一个孩子,我想我不仅可以测试我的Python技能,还可以制作一个互动应用程序,让家人和朋友都能玩得开心

基本上,用户将访问该网站,提供他们的名字,并猜测我的lil one在9月份某个时候交付时的重量

现在,HTML和CSS已经就绪,我正在准备Python脚本。我最终会访问数据库和那些东西,但现在,我有一个障碍,我想先通过

实际发行 当输入名称和权重并单击submit时,用户将被引导到一个“成功”页面,该页面仅感谢他们的猜测,并且该页面将在几秒钟后返回。不幸的是,我不知道几秒钟后如何返回index.html。这就是我需要帮助的地方

以下是index.HTML和success.HTML的HTML:

Index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="stylesheet" href="../static/styles.css" />
    <link
      href="https://fonts.googleapis.com/css2?family=Comic+Neue:wght@300;400;700&display=swap"
      rel="stylesheet"
    />

    <title>Guess Kaden's Weight</title>
  </head>
  <body>
    <div class="container">
      <div class="contents">
        <h1 class="heading" id="main_heading">Guess Kaden's Birth Weight</h1>
        <h3 class="heading desc">
          Kaden's arrival is right around the corner.
        </h3>
        <h3 class="heading desc">
          Submit your guess to what his birth weight is.
        </h3>
        <h3 class="heading desc">Winner gets eternal bragging rights!</h3>
        <h2 class="heading show_avg">Current average weight guessed is:</h2>
        <!-- <div class="message">
          {{text | safe}}
        </div> -->
        <form method="POST" action="{{url_for('success')}}">
          <label for="guesser_name" class="labels"
            >Please provide your name:</label
          >
          <input
            class="input_box"
            id="guesser_name"
            type="text"
            title="guesser_name"
            name="guesser_name"
            required
          />
          <label for="guesser_guess" class="labels"
            >Please provide your guess in pounds:</label
          >
          <input
            class="input_box"
            id="guesser_guess"
            type="number"
            step="0.1"
            title="guesser_guess"
            placeholder="Example - 6.7"
            name="guesser_guess"
            required
          />
          <button type="submit" class="submit_button">Submit</button>
        </form>
      </div>
    </div>
  </body>
</html>

Success.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="stylesheet" href="../static/styles.css" />
    <link
      href="https://fonts.googleapis.com/css2?family=Comic+Neue:wght@300;400;700&display=swap"
      rel="stylesheet"
    />
    <title>Guess Kaden's Weight</title>
  </head>
  <body>
    <div class="success_container">
      <div class="success_contents">
        <h1>
          Thank you for your guess!
        </h1>
        <h1>
          You will now be redirected back to the main screen.
        </h1>
      </div>
    </div>

  </body>
</html>

以及app.py文件:

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)


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


@app.route("/success", methods=['POST'])
def success():
    return render_template("success.html")


if __name__ == "__main__":
    app.debug = True
    app.run()

我试过一些方法,但似乎都不管用。我相信,如果我对烧瓶的工作原理有更多的了解,这将很容易理解,但唉,我对它还是新手

任何意见都将不胜感激


Tags: namedivapp应用程序fortitlehtmlh1