Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, request, flash, redirect, url_for, session, logging
- from wtforms import Form, StringField, PasswordField, FloatField, DateField, validators
- 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('/')
- def index():
- return render_template('home.html')
- @app.route('/home')
- def home():
- return render_template('home.html')
- class RegisterForm(Form):
- name = StringField('Name', [validators.Length(min=1, max=50)])
- email = StringField('Email', [validators.Length(min=6, 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')
- @app.route('/register', methods=['GET', 'POST'])
- def register():
- form = RegisterForm(request.form)
- if request.method == 'POST'and form.validate():
- name = form.name.data
- email = form.email.data
- username = form.username.data
- password = form.password.data
- cursor = mysql.connection.cursor()
- cursor.execute("INSERT INTO users(name, username, password, email), VALUES(name, username, password, email)"), \
- (name, email, username, password)
- mysql.connection.commit()
- cursor.close()
- flash('You are now registered', 'success')
- return redirect(url_for('index'))
- 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()
- if request.method == 'POST' and form.validate:
- username = form.username.data
- password = form.password.data
- cursor = mysql.connection.cursor()
- cursor.execute("SELECT FROM users(username, password) VALUES(%s, %s)"), (username, password)
- mysql.connection.commit()
- cursor.close()
- flash('You are now logged in', 'success')
- return redirect(url_for('index'))
- 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)])
- @app.route('/budgetguard', methods=['GET', 'POST'])
- def budgetguard():
- form = budgetguard()
- if request.method == 'POST':
- month = DateField('month')
- income = FloatField('Income', [validators.Length(max=50)])
- return redirect(url_for('index'))
- if __name__ == '__main__':
- app.secret_key = 'cleopatra'
- app.run(debug=True)
Add Comment
Please, Sign In to add comment