将表单数据从一个 html 页面传递到另一个 html 页面和 flask 中的数据库

Pass form data from one html page to another html page and datase in flask

我愿意填写一个表格(HTML)并将该数据传递到另一个 HTML 页面并将该数据插入数据库。

app.py

@app.route('/signup')
def signup():
    return render_template("client_signup.html")
@app.route('/registered')
def registered():
    userName = request.form['userName']
    userEmail = request.form['userEmail']
    userPassword = request.form['userPassword']
    conn = mysql.connect()
    cursor = conn.cursor()
    cursor.execute("INSERT INTO emp(userName, userEmail, userPassword) VALUES (%s,%s,%s)", (userName, userEmail, userPassword))
    return render_template("client_signin.html")

client_signup.html

<form id="msform" action="registered">
    <!-- progressbar -->
    <ul id="progressbar">
        <li class="active">Account Setup</li>
    </ul>
    <!-- fieldsets -->
    <fieldset>
        <h2 class="fs-title">Create your account</h2>
        <h3 class="fs-subtitle">This is step 1</h3>
        <input type="text" name="userName" placeholder="Your Name" />
        <input type="text" name="UserEmail" placeholder="Email" />
        <input type="password" name="userPassword" placeholder="Password" />
        <input type="password" name="cUserPassword" placeholder="Confirm Password" />
        <input type="submit" name="submit" class="submit action-button" value="Submit" />
    </fieldset>
</form>

我不需要 FLASK 知识(只是初学者)

您可以通过 render_template 函数传递表单数据,并在模板文件中使用 Jinja 语法呈现它们。例如:

@app.route("/registered")
def registered():
    // get form data and insert
    return render_template("client_signin.html", username=username, password=password)

<form>
    <input name="username" value="{{ username }}" />
    <input name="password" value="{{ password }}" />
    <input type="submit" value="submit />
</form>

首先,如果你想做一个POST方法,你需要方法@app.route:

@app.route('/registered', methods=['GET', 'POST'])

那么,对于你的问题,你可以试试代码:

@app.route('/signup')
def signup():
    return render_template("client_signup.html")
@app.route('/registered', methods=['GET', 'POST'])
def registered():
    userName = request.form['userName']
    userEmail = request.form['userEmail']
    userPassword = request.form['userPassword']
    conn = mysql.connect()
    cursor = conn.cursor()
    cursor.execute("INSERT INTO emp(userName, userEmail, userPassword) VALUES (%s,%s,%s)", (userName, userEmail, userPassword))
    return redirect("/signup")

小改动是return redirect("/signup")