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

时间:2018-03-03 21:16:19

标签: mysql flask html-form flask-mysql

我愿意填写表单(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(只是初学者)

2 个答案:

答案 0 :(得分:0)

您可以通过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>

答案 1 :(得分:0)

首先,如果你想制作一个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")