By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Flask function isnt working

Velpus Captiosus

Well-Known Coder
I have this HTML webpage:


        font-family: Verdana, Geneva, Tahoma, sans-serif;

        font-size: medium;


        background-color: aliceblue;

        border-style: hidden;

        shape-outside: border-box;

        cursor: default;

        height: 36px;

        width: 144px;

        border-radius: 9px;

        top: 500px;

        left: 680px;

        position: absolute;






        background-color: orangered;




<title>Sign in/Sign up</title>


<body background="backColorWebsite.png">

<form action="#" method="post">

<input type="text" name="Username" value="Username" style="font-family:Verdana, Geneva, Tahoma, sans-serif; background-color: aliceblue; position: absolute; top:250px;left:680px; width:144px; height:27px; border-radius: 9px; color:gray" maxlength="10"></input>

<input type="password" name="Password" value="Password" style="font-family:Verdana, Geneva, Tahoma, sans-serif; background-color: aliceblue; position: absolute; top:350px;left:680px; width:144px; height:27px; border-radius: 9px; color:gray" maxlength="10"></input>

<button>Sign In</button>



and this python code.

from flask import Flask,render_template,request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)




db = SQLAlchemy(app)

class users(db.Model):

   name = db.Column('name',db.String(100),primary_key=True)
   password = db.Column('password',db.String(100))
   role = db.Column('role',db.String(100))

   def __init__(self,name:str,password:str,role:str):

      self.name = name

      self.password = password

      self.role = role

with app.app_context():


admin = users('Admin','RootRules99','Admin')





def frontPage():

   if request.method=='POST':

      user = request.form['Username']

      password = request.form['Password']

      #found_user = users.query.filter_by(name=user,password=password).first()

      return user,password

      #if found_user:

         #return 'Hello'


      return render_template('mywebsite.html')

I am using the flask library for python Web Programming.If I give out the credentials ['Username':'Admin' , 'Password':'Hi'] the webpage only prints out 'Admin' and it ignores completely the value given at the 'Password' input element.Why is this happenning?
Are you trying to do something like this. I've not worked with alchemy or flask-sessions before.
from flask import Flask, render_template, url_for, session, request, redirect
from flask_session import Session

from sqlalchemy import (create_engine, Column, Integer, String,
                        MetaData, Table, select, insert)
from sqlalchemy_utils import database_exists, create_database
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import sessionmaker

# create the database
engine = create_engine('sqlite:///user.db')
if not database_exists(engine.url):

app = Flask(__name__)
app.config['SESSION_PERMANENT'] = False
app.config['SESSION_TYPE'] = 'filesystem'

# Base class
Base = declarative_base()

# user class
class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    user = Column(String, nullable=False, unique=True)
    password = Column(String, nullable=False)

# Create the tables
# Base.metadata.create_all(engine)

# alchemy session
_Session = sessionmaker(bind=engine)
_session = _Session()

# Create a user
# new_user = User(user='admin', password='mypassword')
# session.add(new_user)
# session.commit()

def index():
    return render_template('index.html')

def form():
    return render_template('form.html')

@app.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        user = request.form.get('user')
        passwd = request.form.get('password')
        que = _session.query(User.user, User.password)
        result = que.all()
        for res in result:
            if res[0] == user and res[1] == passwd:
                session['name'] = res[0]
                return redirect('/')
    return render_template('form.html')

def logout():
    session['name'] = None
    return redirect('/')

if __name__ == '__main__':
I've tinkered with it a little more. Here is what I have.
The send message doesn't handle any messages. It just goes to a confirm page then redirects back to the main page.
Right side displays according to user level.

main folder
-- static
-- app.py

app.py Note: I created tables and users then commented them out

There are several files so, I will attach instead of posting everyone of them. Easier 😉


New Threads

Latest posts

Buy us a coffee!

Top Bottom