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>

&lt;head&gt;

    &lt;link rel="stylesheet" href="../static/form-style.css"&gt;



    &lt;script&gt;

    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;

    }

    }



    &lt;/script&gt;



&lt;/head&gt;

&lt;body&gt;

    &lt;div&gt;

            &lt;form name="login-form"  action="/login" onsubmit="return validateForm()" target="_self" method="POST"  &gt;

                &lt;h2&gt;Login:&lt;/h2&gt;


                &lt;input type ="text" name ="username" placeholder="Username"&gt;&lt;br&gt;

                &lt;input type="password" name="password" placeholder="Password"&gt;&lt;br&gt;

                &lt;input type="submit" value="Submit"&gt;&lt;br&gt;

            &lt;/form&gt;

    &lt;/div&gt;

&lt;/body&gt;

</html>

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

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Flask for Beginners: Build a CRUD Web App with Python and Flask

In this Python Flask tutorial, you'll learn to build CRUD web applications using Python and Flask. Python and Flask can make building a CRUD app super easy.

Getting Started with Flask - Python Flask Tutorial for Beginners

In this Python Flask tutorial for beginners, we will see how to get started with Flask the Python Microframwork, its installation and a 'hello world' example and create awesome web apps!

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...