login.html
{% extends "layout.html" %} {% block content %} <div class="form"> <h2>Sign In</h2> {% for field in form.errors %} {% for error in form.errors[field] %} <div class="alert alert-error"> <strong>Oops...</strong> {{error}}. </div> {% endfor %} {% endfor %} <form action="{{ url_for('login') }}" method=post> {{ form.hidden_tag() }} {{ form.email.label }} {{ form.email }} {{ form.password.label }} {{ form.password }} <p> <input id="submit" name="submit" type="submit" class="btn btn-inverse" value="Sign In"></p> </form> </div> {% endblock %}
forms.py
class SigninForm(Form): email = TextField("email", [validators.Required("Please enter your email")]) password = PasswordField('Password', [validators.Required("Please enter a password.")]) submit = SubmitField("Sign In")
Then import signinform into your views and create your login method like this
@app.route('/login', methods=['GET', 'POST']) def signin(): form = SigninForm() if form.validate_on_submit(): session['email'] = form.email.data flash('You are logged in') return redirect(url_for('dashboard')) return render_template('signin.html', form=form)
Refer to this guide for more detailed instructions http://pypix.com/python/building-flask-blog-part-1/
source share