'Flask-Login: Redirect to page after login
**The problem was in this code lgin enter image description here
You only needed to delete 1 line (<form metod="POST" action="/singup") to make everything work, thank you @PatrickYoder for your help**
I'm making a site with 3 links (Home, Regestration, login-in).
When you go to the "Home" link, the transition takes place to the address "ttp://127.0.0.1:5002/".
If you go to "Regestration" opens a page with 2 input fields (Create username,Create password) and the "Submit" button. If you enter the Name, Password (For example (name:5 password:5)) press the button, the data is entered into the sqlite database (password hashed).
And translates to the page ttp://127.0.0.1:5002/eee (where the text "Hello its work" is written)
But if you go to the Login page (ttp://127.0.0.1:5002/lgin) where everything is similar to the registration page.
When you enter login name 5 password 5 then the transition takes place to the page (ttp://127.0.0.1:5002/signup) but expected to go to (ttp://127.0.0.1:5002/eee
import os
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
import logging
import sqlite3
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
def check_password(hashed_password, user_password):
return hashed_password == hashlib.md5(user_password.encode()).hexdigest()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
password = db.Column(db.String(128), index=True, unique=True)
def __repr__(self):
return '<User {}>'.format(self.username)
@app.route('/')
def hello():
return render_template('index.html')
@app.route('/eee', methods=['post', 'get'])
def ee():
return render_template('eee.html')
@app.route('/registration', methods=['post', 'get'])
def hel():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter_by(username=username).first()
if user:
return redirect (url_for('hel'))
new_user = User(username=username, password=generate_password_hash(password, method='sha256'))
db.session.add(new_user)
db.session.commit()
return redirect(url_for('ee'))
return render_template('registration.html')
@app.route('/lgin', methods=['post', 'get'])
def logi():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
con = sqlite3.connect('site.db')
completion = False
with con:
cur = con.cursor()
cur.execute("SELECT * FROM User")
rows = cur.fetchall()
for row in rows:
dbUser = row[0]
dbPass = row[1]
if dbUser==username:
completion=check_password(dbPass, password)
return redirect(url_for('ee'))
return render_template('lgin.html',)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
