Skip to content

Commit

Permalink
login part
Browse files Browse the repository at this point in the history
  • Loading branch information
FengWeilei committed Jul 30, 2017
1 parent 920472f commit 26667c5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 5 deletions.
Binary file added flaskapp/__pycache__/config.cpython-35.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions flaskapp/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CSRF_ENABLED = True
SECRET_KEY = "It doesn't matter"
63 changes: 58 additions & 5 deletions flaskapp/flaskapp.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
from flask import Flask,render_template
from flask import Flask,render_template,request,flash,redirect,url_for
from wtforms import StringField, SubmitField,validators, PasswordField
from flask_wtf import FlaskForm
from flask_mysqldb import MySQL
from passlib.hash import sha256_crypt

app = Flask(__name__)



#Config to mysql
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'abc230002'
app.config['MYSQL_DB'] = 'FLASKAPP'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'

#Initialize the extension
mysql = MySQL(app)

@app.route('/')
def index():
return render_template("index.html")

@app.route('/<name>')
def hello(name):
return "Hello,%s" % name

class RegisterForm(FlaskForm):
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_on_submit():
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()

flash("You are now registered.Please log in.",'success')

return redirect(url_for('index'))

return render_template('register.html',form=form)

#CSRF
#app.config.from_object('config')

if __name__ == "__main__":
app.run(debug=True)
app.secret_key="It doesn't matter"
app.run(debug=True)
12 changes: 12 additions & 0 deletions flaskapp/templates/includes/_formhelpers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
7 changes: 7 additions & 0 deletions flaskapp/templates/includes/_messages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}" >{{ message }}</div>
{% endfor %}
{% endif %}
{% endwith %}
3 changes: 3 additions & 0 deletions flaskapp/templates/includes/_navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<li><a href="/about">About</a></li>
<li><a href="/articles">Articles</a></li>
</ul>
<ul class="nav navbar-right">
<li class="active"><a href="/register">Register</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
2 changes: 2 additions & 0 deletions flaskapp/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
{% include 'includes/_navbar.html' %}

<div class="container">
{% include 'includes/_messages.html' %}
{% block body%}{% endblock %}

</div>

<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
Expand Down
25 changes: 25 additions & 0 deletions flaskapp/templates/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends 'layout.html' %}

{% block body %}
<h1>Register</h1>
{% from "includes/_formhelpers.html" import render_field %}
<form method="POST",action="">
{{ form.csrf_token }}
<div class="form-group">
{{render_field(form.name,class_="form-control")}}
</div>
<div class="form-group">
{{render_field(form.email, class_="form-control")}}
</div>
<div class="form-group">
{{render_field(form.username, class_="form-control")}}
</div>
<div class="form-group">
{{render_field(form.password, class_="form-control")}}
</div>
<div class="form-group">
{{render_field(form.confirm, class_="form-control")}}
</div>
<p><input type="submit" class="btn btn-primary" value="Submit"></p>
</form>
{% endblock %}

0 comments on commit 26667c5

Please sign in to comment.