How to Sending form data to a template on flask

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?

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?

Python:

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


@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()


def query_db(query, args=(), one=False): #connects html with
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    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
    else:
        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("/")
@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'])
        else:
            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__":
    app.run()

Html:

<html>

    <head>

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



        <script>

        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;

        }

        }



        </script>



    </head>

    <body>

        <div>

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

                    <h2>Login:</h2>


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

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

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

                </form>

        </div>

    </body>

</html>

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

python html flask

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Know Everything About HTML With HTML Experts

HTML Assignment Help Australia @30% OFF from Sample Assignment, with Our Best HTML assignment help experts. Get HTML homework help online at affordable price. 100% Plag free assignment solution.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Python Flask - Introduction to Flask Templates

This is our second tutorial in Python Flask, in this tutorial we are going to have Introduction to Flask Templates, so for this Flask looks for the template

Python Flask-Mail Library to Send Emails in Browser Using Flask Full Project For Beginners

Python Flask-Mail Library to Send Emails in Browser Using Flask Full Project For Beginners #python #flask #flaskmail Welcome Folks My name is Gautam