烧瓶表格提交问题

时间:2019-08-20 07:00:34

标签: flask

我的index.html上有一个表单,我有一个app.py来处理它,但是它什么也没做。未处理数据,并且不会发生重定向。按下提交按钮没有任何作用,因此好像app.py无法正常工作,但我正在运行app.py,而Im正在使用http://127.0.0.1:5000/。在我的终端中,app.py正在运行,没有任何错误。

app.py

from flask import Flask, render_template, request

app = Flask(__name__)


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


@app.route('/submit', methods=['POST'])
def submit():
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        phone = request.form['phone']
        message = request.form['message']
        print(name, email, phone, message)
        return render_template('success.html')


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

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script src="https://kit.fontawesome.com/b015121141.js"></script>
  <link rel="stylesheet" href="../static/css/style.css">
  <link rel="stylesheet" media="screen and (max-width: 768px)" href="../static/css/mobile.css">
  <link rel="stylesheet" media="screen and (min-width: 1100px)" href="../static/css/widescreen.css">
  <title>System Services</title>

</head>

<body id="home">

  <!-- Section: Contact-->
  <section id="contact">
    <div class="contact-form bg-primary p-2">
      <h2 class="m-heading">Contact Us</h2>
      <p>Please Use the form below to contact us</p>
      <form action="/submit" method="POST">
        <div class="form-group">
          <label for="name">Name</label>
          <input type="text" name="name" id="name" placeholder="Enter Name">
        </div>
        <div class="form-group">
          <label for="email">Email</label>
          <input type="email" name="email" id="email" placeholder="Enter Email">
        </div>
        <div class="form-group">
          <label for="phone">Phone</label>
          <input type="text" name="phone" id="phone" placeholder="Enter Phone">
        </div>
        <div class="form-group">
          <label for="message">Message</label>
          <textarea name="message" id="message" placeholder="Enter Message"></textarea>
        </div>
        <input type="submit" value="Submit" class="btn btn-dark">
      </form>
    </div>
    <div class="map"></div>
  </section>

  <!-- Footer -->
  <footer id="main-footer" class="bg-dark text-center py-1">
    <div class="container">
      <p>Copyright &copy; 2019, All Rights Reserved</p>
    </div>
  </footer>


</body>

</html>

2 个答案:

答案 0 :(得分:0)

我运行了您的代码,工作正常。我在您的视图中检查了您的POST值,它们都在那里。我得到的唯一错误是success.html不存在,实际上并不存在。在您的模板文件夹中创建success.html文件,就可以了。

答案 1 :(得分:0)

您添加sucess.html的代码就可以正常工作

如果还有其他问题,我邀请您检查我的一个使用django POST方法的项目(有据可查),并生成一些内容(在我的情况下是带有用户数据的.docx文件):Flask Docx Generator < / p>

如果这个项目非常有用,您可以投票支持我的答案,并将其作为您问题的答案:)