同一页上的Python电子邮件验证

2024-04-26 07:31:40 发布

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

我想做一个简单的电子邮件验证应用程序。它只需要页面的索引和成功页面,列出所有的电子邮件。有三个路由:索引路由、验证路由,最后是列出实际电子邮件的成功路由。这是我的密码:

from flask import Flask, request, redirect, render_template, session
import datetime
from mysqlconnection import MySQLConnector

app = Flask(__name__)
mysql = MySQLConnector(app,'friendsdb')

@app.route('/', methods=['POST','GET'])
def index():
    if request.method=='GET':
        return render_template('index.html')
    elif request.method=='POST':
        friends = mysql.query_db("SELECT * FROM friends")
        print friends
        print request.form['first_name'],
        print request.form['last_name'],
        print request.form['Age']
        return render_template('index.html')

#@app.route('/delete/<friend_id>', methods=['POST'])
#def delete(friend_id):
    #query = "DELETE FROM friends WHERE id = :id"
    #data = {'id': friend_id}
    #mysql.query_db(query, data)
    #return redirect('/')

app.run(debug=True)

以及我的html:

<!DOCTYPE html>
<head>
  <h1>Friends</h1>
</head>
<body>
  <form action="/" methods="GET">
    <table>
      <th>Name:</th>
      <th>Age:</th>
      <th>Friend Since:</th>
      {% for friends in all_friends: %}
      <tr>ID:{{friends['id']}}</tr>
      <tr>First Name:{{friends['first_name']}}Last Name:{{friends['last_name']}}</tr>
      <tr>Age:{{friends['age']}}</tr>
      <tr>Date:{{friends['NOW();']}}</tr>
      <tr><form action="/delete" method="POST">
        <p>Delete a friend</p> <input type="submit"></tr>
      {%endfor%}
  </form>
    </table>

    <h1>Add a Friend</h1>
  <form action="/" method="POST">
   <p>First Name:</p><input type="text" name="first_name">
   <p>Last Name:</p><input type="text" name="last_name">
   <p>Age</p><input type="text" name="age">
   <input type="submit">
  </form>


  </form>
</body>

我正试着把数据从输入表输入到上面有什么建议吗?你知道吗


Tags: nameformidapp路由inputrequesthtml
1条回答
网友
1楼 · 发布于 2024-04-26 07:31:40

下面是一个最简单的示例,演示如何进行更改,您所做的更改位于render\u template行render_template("index.html", friends=friends)

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def index():
    friends = [
        {'name': 'alice', 'age': '7'},
        {'name': 'bob', 'age': '13'},
        {'name': 'eve', 'age': '42'}
    ]
    return render_template("index.html", friends=friends)

if __name__ == "__main__":

    app.run(debug=True)

还有HTML

<!DOCTYPE html>
<head>
  <title>Friends</title>
</head>
<body>

    <table>
        <tr>
            <th>Name:</th>
            <th>Age:</th>
        </tr>

        {% for friend in friends %}
        <tr>
            <td>{{friend['name']}}<td>
            <td>{{friend['age']}}<td>
        </tr>
        {% endfor %}
    </table>

</body>

相关问题 更多 >