Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # app.py
- from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
- # from flask_sqlalchemy import SQLAlchemy
- # from data import Articles
- from data import Articles
- from flask_mysqldb import MySQL
- from wtforms import Form, StringField, TextAreaField, PasswordField, validators
- from passlib.hash import sha256_crypt
- app = Flask(__name__)
- # app.config['SECRET_KEY'] = 'xxxxxxxxxx'
- # Config MySQL
- app.config['MySQL_HOST'] = 'localhost'
- app.config['MySQL_USER'] = 'root'
- app.config['MySQL_PASSWORD'] = ''
- app.config['MySQL_DB'] = 'myflaskapp'
- app.config['MySQL_CURSORCLASS'] = 'DictCursor'
- # Init MySQL
- mysql = MySQL(app)
- # Setting articles
- Articles = Articles()
- @app.route('/')
- def home():
- return render_template('home.html')
- @app.route('/about')
- def about():
- return render_template('about.html')
- @app.route('/articles')
- def articles():
- return render_template('articles.html', articles=Articles)
- @app.route('/article/<string:id>/')
- def article(id):
- return render_template('article.html', id=id)
- class RegisterForm(Form):
- name = StringField('Name', [validators.Length(min=1, max=50)])
- username = StringField('Username', [validators.Length(min=4, max=25)])
- email = StringField('Email', [validators.Length(min=6, max=50)])
- password = PasswordField('Password', [validators.DataRequired(),
- validators.EqualTo(
- 'confirm', message='Passwords do not match!')
- ])
- 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 = sha256_crypt.encrypt(str(form.password.data))
- # Creat Cursor
- cur = mysql.connection.cursor()
- # Execute Query
- cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)",
- (name, email, username, password))
- # Commit to DB
- mysql.connection.commit()
- # close connection
- cur.close()
- # Message to user once registered
- flash('You are now registered and can login, thank you', 'success')
- return redirect(url_for('index'))
- return render_template('register.html', form=form)
- if __name__ == '__main__':
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement