Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, request, flash, redirect, url_for
- from wtforms import Form, StringField, PasswordField, FloatField, DateField, validators, form
- from flask_mysqldb import MySQL
- app = Flask(__name__)
- app.config['MYSQL_HOST'] = 'localhost'
- app.config['MYSQL_USER'] = 'root'
- app.config['MYSQL_PASSWORD'] = 'Dream1234'
- app.config['MYSQL_DB'] = 'myflaskapp'
- app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
- mysql = MySQL(app)
- @app.route('/home')
- def home():
- return render_template('home.html')
- class RegisterForm(Form):
- name = StringField('Name', [validators.Length(min=1, max=50)])
- username = StringField('Username', [validators.Length(min=4, max=50)])
- password = PasswordField('Password', [
- validators.DataRequired(),
- validators.EqualTo('confirm', message='Passwords are different')])
- confirm = PasswordField('Confirm Password')
- email = StringField('Email', [validators.Length(min=6, max=50)])
- @app.route('/register', methods=['GET', 'POST'])
- def register():
- form = RegisterForm(request.form)
- if request.method == 'POST' and form.validate():
- name = form.name.data
- username = form.username.data
- password = form.password.data
- email = form.email.data
- cursor = mysql.connection.cursor()
- cursor.execute("INSERT INTO users(name, username, password, email) VALUES(%s, %s, %s, %s)",
- (name, username, password, email))
- mysql.connection.commit()
- cursor.close()
- flash('You are now registered', 'success')
- return redirect(url_for('budgetguard'))
- return render_template('register.html', form=form)
- class Login(Form):
- username = StringField('Username', [validators.Length(min=4, max=50)])
- password = PasswordField('Password', [validators.DataRequired()])
- @app.route('/login', methods=['GET', 'POST'])
- def login():
- form = Login(request.form)
- if request.method == 'POST' and form.validate:
- username = form.username.data
- password = form.password.data
- cursor = mysql.connection.cursor()
- cursor.execute("SELECT * FROM users where username = %s and password = %s", (username, password))
- mysql.connection.commit()
- user = cursor.fetchone()
- cursor.close()
- if user is None:
- flash('No user found with these details.', 'danger')
- # print(user)
- else:
- flash('You are now logged in', 'success')
- return redirect(url_for('budgetguard'))
- return render_template('login.html', form=form)
- class BudgetGuard(Form):
- month = DateField('Month')
- income = FloatField('Income', [validators.Length(max=50)])
- expenses = FloatField('Expenses', [validators.Length(max=45)])
- balance = FloatField('Balance',)
- def sub():
- form = BudgetGuard()
- income = form.income.data
- expenses = form.expenses.data
- balance = income - expenses
- return balance
- income = 0
- expenses = 0
- balance = 0
- next_page = 0
- @app.route('/budgetguard', methods=['GET', 'POST'])
- def budgetguard():
- global balance
- form = BudgetGuard(request.form)
- if request.method == 'POST' and form.validate:
- income = form.income.data
- expenses = form.expenses.data
- balance = income - expenses
- return render_template('budgetguard.html', form=form, balance=balance)
- if __name__ == '__main__':
- app.secret_key = 'cleopatra'
- app.run(debug=True)
Add Comment
Please, Sign In to add comment