烧瓶上的注册页面

时间:2020-05-27 08:09:55

标签: python html postgresql flask

虽然使用flask创建了一个注册页面,并使用postgreSQL存储了凭据,但是凭据并未存储在数据库中,这对flask还是陌生的。

这是html,我想将此表单提交到flask应用程序中的重定向功能以获取用户名和密码

<form action=" {{ url_for('redirection') }}" method="post">
    <label for="username"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="username" required><br>

    <label for ="password"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="password" required><br>



    <button type="button" class="btn btn-primary">Submit</button>
</form>

这是烧瓶应用程序,

import os

from flask import Flask, session, render_template,request
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

app = Flask(__name__)

# Check for environment variable
if not os.getenv("DATABASE_URL"):
    raise RuntimeError("DATABASE_URL is not set")

# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))


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

@app.route("/register")
def register():
    return render_template("registration.html")

@app.route("/login")
def login():
    return render_template("login.html") 

@app.route("/redirection", methods = ['POST'])
def redirection():
    username = request.form.get("username")
    password = request.form.get("password")
    db.execute("INSERT INTO users(username, password) VALUES (:username, :password)", {"username": username, "password": password})
    db.commit()
    return render_template("index.html")

1 个答案:

答案 0 :(得分:0)

首先,您必须从烧瓶url_for导入

from flask import Flask, session, render_template, request, url_for # add the url_for

第二件事,我认为您需要将函数HTML发送为参数,因此请尝试

return render_template("login.html", redirection=url_for('redirection')) 

并在HTML页面内

<form action="{{redirection}}" method="post">....