Browse Source

Chapter 8: User registration (8d)

master
T. Meissner 6 years ago
parent
commit
869a8b0448
5 changed files with 64 additions and 3 deletions
  1. +25
    -1
      app/auth/forms.py
  2. +16
    -1
      app/auth/views.py
  3. +7
    -0
      app/templates/auth/login.html
  4. +13
    -0
      app/templates/auth/register.html
  5. +3
    -1
      app/templates/index.html

+ 25
- 1
app/auth/forms.py View File

@ -1,6 +1,8 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, Length, Email
from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo
from wtforms import ValidationError
from ..models import User
class LoginForm(FlaskForm):
@ -9,3 +11,25 @@ class LoginForm(FlaskForm):
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Keep me logged in')
submit = SubmitField('Log in')
class RegistrationForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Length(1, 64,
Email())])
username = StringField('Username', validators=[
DataRequired(), Length(1, 64),
Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
'Usernames must have only letters, numbers, dots or '
'underscores')])
password = PasswordField('Password', validators=[
DataRequired(), EqualTo('password2', message='Passwords must match.')])
password2 = PasswordField('Confirm password', validators=[DataRequired()])
submit = SubmitField('Register')
def validate_email(self, field):
if User.query.filter_by(email=field.data).first():
raise ValidationError('Email already registered.')
def validate_username(self, field):
if User.query.filter_by(username=field.data).first():
raise ValidationError('Username already in use.')

+ 16
- 1
app/auth/views.py View File

@ -1,8 +1,9 @@
from flask import render_template, redirect, request, url_for, flash
from flask_login import login_user, logout_user, login_required
from . import auth
from .. import db
from ..models import User
from .forms import LoginForm
from .forms import LoginForm, RegistrationForm
@auth.route('/login', methods=['GET', 'POST'])
@ -26,3 +27,17 @@ def logout():
logout_user()
flash('You have been logged out.')
return redirect(url_for('main.index'))
@auth.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(email=form.email.data,
username=form.username.data,
password=form.password.data)
db.session.add(user)
db.session.commit()
flash('You can now login.')
return redirect(url_for('auth.login'))
return render_template('auth/register.html', form=form)

+ 7
- 0
app/templates/auth/login.html View File

@ -9,5 +9,12 @@
</div>
<div class="col-md-4">
{{ wtf.quick_form(form) }}
<br>
<p>
New user?
<a href="{{ url_for('auth.register') }}">
Click here to register
</a>
</p>
</div>
{% endblock %}

+ 13
- 0
app/templates/auth/register.html View File

@ -0,0 +1,13 @@
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Flasky - Register{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>Register</h1>
</div>
<div class="col-md-4">
{{ wtf.quick_form(form) }}
</div>
{% endblock %}

+ 3
- 1
app/templates/index.html View File

@ -4,6 +4,8 @@
{% block page_content %}
<div class="page-header">
<h1>Hello, {% if current_user.is_authenticated %}{{ current_user.username }}{% else %}Stranger{% endif %}!</h1>
<h1>
Hello, {% if current_user.is_authenticated %}{{ current_user.username }}{% else %}Stranger{% endif %}!
</h1>
</div>
{% endblock %}

Loading…
Cancel
Save