How to Sending form data to a template on flask

I’m trying to create a login page and have managed to get each separate page working but when you click “Login” it should take you to a profile page and when you click “Register” it should take you back to login. This does not happen and instead comes up with the ‘can’t reach this page’. How can I use flask to send the data from the template and render a new page?


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

app = Flask(__name__)

DATABASE = './users.db'

def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:

def query_db(query, args=(), one=False): #connects html with
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    return (rv[0] if rv else None) if one else rv

def valid_login(username, password):
    user = query_db('select * from User where username = ? and password = ?', [username, password], one=True)
    if user is None:
        return False
        return True

def log_the_user_in(username):
    return render_template('profile.html', username=username)

def register_user(name, email, username, password):
    query_db('INSERT INTO users (username, password, email, name) VALUES' '(%s, %s, %s, %s)',
             (username, password, email, name))

@app.route('/login', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if valid_login(request.form['username'], request.form['password']):
            return log_the_user_in(request.form['username'])
            error = 'Invalid username/password'

    return render_template('login.html', error=error)

@app.route('/register', methods=['POST', 'GET'])
def register():
    if request.method == 'POST':
        register_user(request.form['name'], request.form['email'], request.form['username'], request.forn['password'])
        return render_template('login.html', error=None)

    return render_template('Register.html', error=None)

if __name__ == "__main__":




        <link rel="stylesheet" href="../static/form-style.css">


        function validateForm() {

        var x = document.forms["login-form"]["username"].value;

        if (x==null || x=="") {

            alert("Name must be filled out");

            return false;


        var y = document.forms["login-form"]["password"].value;

        if (y==null || y=="") {

            alert("Password name must be filled out");

            return false;







                <form name="login-form"  action="/login" onsubmit="return validateForm()" target="_self" method="POST"  >


                    <input type ="text" name ="username" placeholder="Username"><br>

                    <input type="password" name="password" placeholder="Password"><br>

                    <input type="submit" value="Submit"><br>





Does anyone have a clue about how to edit? Thanks in advance.

#python #html #flask

How to Sending form data to a template on flask
2 Likes60.95 GEEK