sqlite 如何访问Flask中的另一个页面

xzabzqsa  于 2023-06-30  发布在  SQLite
关注(0)|答案(2)|浏览(128)

我有三个网页主页和添加页和结果页我可以访问主页但当我点击按钮访问添加页我不能
下面是HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="/static/css/main.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
    <title>Fen</title>
</head>
<body>
<section>
    <div class="form-box">
        <div class="form-value">
            <!-- This should be post method and the action should be the desired route -->
            <form method="POST" action="/">
                <h2>Add Phone</h2>
                <div class="inputbox">
                    <ion-icon name="mail-outline"></ion-icon>
                    <input name="name" id="name" type="text">
                    <label for="">Name</label>
                </div>
                <div class="inputbox">
                    <ion-icon name="mail-out-line"></ion-icon>
                    <input name="phone_name" id="phone_name" type="text">
                    <label for="">Phone Name</label>
                </div>
                <div class="inputbox">
                    <ion-icon name="lock-closed-outline"></ion-icon>
                    <input name="serial_number" type="text" id="sr" required>
                    <label for="">Serial number</label>
                </div>   
                <div class="inputbox">
                    <ion-icon name="lock-closed-outline"></ion-icon>
                    <input name="email" type="email" id="em" required>
                    <label for="">E-mail</label>
                </div>   
                <div class="inputbox">
                    <ion-icon name="lock-closed-outline"></ion-icon>
                    <input name="number" type="text" id="pn" required>
                    <label for="">Phone Number</label>
                </div>
                <button id="log" type="submit">Add Phone</button>
            </form>
        </div>
    </div>
</section>
<script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"></script>
</body>
</html>

下面是python/flask代码:

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

app = Flask(__name__)

def create_close(connection):
    try:
        connection.commit()
        connection.close()
        print("Closed database successfully and saved the changes")
    except:
        print("Unable to close the database") 

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

@app.route('/add')
def main():
    # Create the table if it doesn't exist
    connection = sqlite3.connect('phonebook2.db')
    cursor = connection.cursor()

    # Creating the query for adding a table in the database
    create_table_query = '''
        CREATE TABLE IF NOT EXISTS phonebook2 (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT,
            number TEXT
        )
    '''
    cursor.execute(create_table_query)
    create_close(connection)
    return render_template('add_button.html')

@app.route('/add', methods=['POST'])
def phonebook():
    name = request.form['name']
    phonenumber = request.form['number']
    connection = sqlite3.connect('phonebook2.db')
    cursor = connection.cursor()
    query1 = "INSERT INTO phonebook2 (name, number) VALUES (?, ?)"
    cursor.execute(query1, (name, phonenumber))
    create_close(connection)

    # After submit, redirecting to the result page
    return redirect("/result?name=" + name)

@app.route('/result', methods=['GET'])
def result():
    try:
        name = request.args.get('name')
        connection = sqlite3.connect('phonebook2.db')
        cursor = connection.cursor()
        query2 = "SELECT number from phonebook2 WHERE name = ?"
        result = cursor.execute(query2, (name,))
        result = result.fetchone()
        create_close(connection)

        if result:
            phonenumber = result[0]
        else:
            phonenumber = ""
        
        return render_template('result.html', n=name, p=phonenumber)
    
    except:
        return render_template('result.html', n="", p="")

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

我试图改变网址,也是我在 flask begginer仍在努力学习
谢谢你想帮我

odopli94

odopli941#

问题可能是您的表单操作URL在HTML中是错误的。现在,它被设置为action="/",这意味着表单数据将被提交到您的主页路由。
查看您的Python代码,您想要处理POST请求的路由是/add。您应该将表单操作更改为action="/add"
以下是HTML代码的更新部分:

<form method="POST" action="/add">
  <h2>Add Phone</h2>
  <!-- Rest of your form -->
  <button id="log" type="submit">Add Phone</button>
</form>

另外,请确保add_button.html文件位于templates目录中,因为Flask的render_template函数在默认情况下会在名为templates的文件夹中查找模板。

vfwfrxfs

vfwfrxfs2#

<form method="POST" action="/add">
  <h2>Add Phone</h2>
  <!-- Rest of your form -->
  <button id="log" type="submit">Add Phone</button>
</form>

如果你使用的是tag,那么使用tag中的属性action,并使用你创建的函数的路由名称来运行下一步。

相关问题