• 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:

HTML:
<style>

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

        font-size: medium;

        color:black;

        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;



       



       

       

       


    }
    button:hover
   
    {

        background-color: orangered;


    }


</style>

<head>

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

</head>

<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>

    </form>


</body>

and this python code.

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


app = Flask(__name__)

app.app_context().push()

app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///db.sqlite3'

app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False


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():

   db.create_all()

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

loggedInUsr:users

db.session.add(admin)

db.session.commit()

@app.route('/',methods=['POST','GET'])

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'







   else:

      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.
Python:
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):
    create_database(engine.url)

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

# 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()




@app.route('/')
def index():
    return render_template('index.html')

@app.route('/form')
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')

@app.route('/logout')
def logout():
    session['name'] = None
    return redirect('/')

if __name__ == '__main__':
    app.run(debug=True)
 
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.

Structure
main folder
-- static
----css
------base.css
--templates
----about.html
----base.html
----confirm.html
----contact.html
----form.html
----index.html
-- 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 ;)
 

Attachments

  • flaskit.zip
    6.8 KB · Views: 0

New Threads

Latest posts

Buy us a coffee!

300x250
Top Bottom