Untitled
unknown
python
2 years ago
2.3 kB
6
Indexable
# app.py from flask import Flask, request, render_template, redirect, url_for from flask_sqlalchemy import SQLAlchemy from werkzeug.security import generate_password_hash, check_password_hash app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db' db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) def __init__(self, username, email, password): self.username = username self.email = email self.password = generate_password_hash(password) @app.route('/') def index(): return render_template('index.html') @app.route('/signup', methods=['GET', 'POST']) def signup(): if request.method == 'POST': username = request.form['username'] email = request.form['email'] password = request.form['password'] user = User(username=username, email=email, password=password) db.session.add(user) db.session.commit() return redirect(url_for('login')) return render_template('signup.html') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': email = request.form['email'] password = request.form['password'] user = User.query.filter_by(email=email).first() if user and check_password_hash(user.password, password): return redirect(url_for('dashboard')) else: return 'Invalid email or password' return render_template('login.html') @app.route('/dashboard') def dashboard(): return 'Welcome to the dashboard' if __name__ == '__main__': db.create_all() app.run(debug=True) <!-- templates/index.html --> <h1>Welcome to the app</h1> <a href="{{ url_for('signup') }}">Sign Up</a> <a href="{{ url_for('login') }}">Login</a> <!-- templates/signup.html --> <h1>Sign Up</h1> <form action="{{ url_for('signup') }}" method="post"> <label for="username">Username:</label> <input type="text" name="username" id="username"> <label for="email">Email:</label> <input type="email" name="email"
Editor is loading...