Hi,
i'm new to this whole thing. I'm trying to create a project. For the UI i want a webpage and i'm sitting on that page for longer then i wanted... The problem is not that i have much to do (that too) but i don't make any progress since i ran in a problem i can't resolve...
so i have a structure:
-Project
--UI
---static
----img
-----favicon.png
-----logo.png
----scripts
-----dropdown.js
----styles.css
---templates
----index.html
----start_desktop.html
----content
-----change_pw.html
----nav
-----nav.html
-----nav_admin.html
---app.py
---auth.py
---routes.py
---utils.py
The code of the app.py looks like this:
My question now is... WHY??? i can't find the error in my logic...
Can you please help me?
Thank you very much,
dissi :)
i'm new to this whole thing. I'm trying to create a project. For the UI i want a webpage and i'm sitting on that page for longer then i wanted... The problem is not that i have much to do (that too) but i don't make any progress since i ran in a problem i can't resolve...
so i have a structure:
-Project
--UI
---static
----img
-----favicon.png
-----logo.png
----scripts
-----dropdown.js
----styles.css
---templates
----index.html
----start_desktop.html
----content
-----change_pw.html
----nav
-----nav.html
-----nav_admin.html
---app.py
---auth.py
---routes.py
---utils.py
The code of the app.py looks like this:
from flask import Flask
from flask_bcrypt import Bcrypt
import secrets
import sqlite3
import os
app = Flask(__name__)
app.secret_key = secrets.token_hex(16)
bcrypt = Bcrypt(app)
app.static_folder = 'static'
app.template_folder = 'templates'
def create_admin_user():
conn = sqlite3.connect('financepi.db')
cursor = conn.cursor()
cursor.execute(
'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, password TEXT, firstlogin BOOLEAN)')
cursor.execute('SELECT COUNT(*) FROM users WHERE username=?', ('admin',))
if cursor.fetchone()[0] == 0:
hashed_password = bcrypt.generate_password_hash('admin').decode('utf-8')
cursor.execute('INSERT INTO users (username, password, firstlogin) VALUES (?, ?, ?)',
('admin', hashed_password, 1))
conn.commit()
conn.close()
def create_db():
if not os.path.exists('financepi.db'):
create_admin_user()
if __name__ == '__main__':
create_db()
app.run(debug=True)The routes looks like this:from flask import render_template, request, redirect, url_for, session
from app import app
from auth import login, logout, check_admin
from utils import create_user, change_password
@app.route('/')
def index():
return render_template('index.html')
auth.py looks like:
from flask import render_template, redirect, url_for, session
from app import bcrypt
import sqlite3
def login(username, password):
if check_login(username, password):
session['username'] = username
session['firstlogin'] = check_first_login(username)
return redirect(url_for('start'))
else:
return render_template('index.html', error=True)
def logout():
session.clear()
return redirect(url_for('index'))
def check_admin():
if 'username' in session:
username = session['username']
if username == 'admin':
return True
return False
# Funktionen für die Authentifizierung
def check_login(username, password):
conn = sqlite3.connect('financepi.db')
cursor = conn.cursor()
cursor.execute('SELECT password FROM users WHERE username = ?', (username,))
user = cursor.fetchone()
conn.close()
if user and bcrypt.check_password_hash(user[0], password):
return True
else:
return False
def check_first_login(username):
conn = sqlite3.connect('financepi.db')
cursor = conn.cursor()
cursor.execute('SELECT firstlogin FROM users WHERE username = ?', (username,))
first_login = cursor.fetchone()[0]
conn.close()
return first_login == 1now when i want to load the page i get an error 404.My question now is... WHY??? i can't find the error in my logic...
Can you please help me?
Thank you very much,
dissi :)
Larz60+ write Mar-21-2024, 10:28 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags have been added this time.
when posting errors please include verbatium errormessage using error tags
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags have been added this time.
when posting errors please include verbatium errormessage using error tags
