Created
September 24, 2021 21:26
-
-
Save dominiksimgen/b58e00a9d0fe00b961051210da723bd0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# basic login form | |
# main.py | |
from flask import Flask, render_template | |
from flask_wtf import FlaskForm | |
from wtforms import StringField, PasswordField, SubmitField | |
class LoginForm(FlaskForm): | |
email = StringField(label='Email') | |
password = PasswordField(label='Password') | |
submit = SubmitField(label="Log in") | |
app = Flask(__name__) | |
app.secret_key = "test-secret" | |
@app.route("/") | |
def home(): | |
return render_template('index.html') | |
@app.route("/login") | |
def login(): | |
login_form = LoginForm() | |
return render_template('login.html', form=login_form) | |
if __name__ == '__main__': | |
app.run(debug=True) | |
# login.html | |
... | |
<form method="POST" action="{{ url_for('login') }}"> | |
{{ form.csrf_token }} | |
{{ form.email.label }} {{ form.email(size=30) }} | |
{{ form.password.label }} {{ form.password(size=30) }} | |
{{ form.submit }} | |
</form> | |
... | |
# login form with input validation (email format and password length) | |
# main.py | |
from flask import Flask, render_template | |
from flask_wtf import FlaskForm | |
from wtforms import StringField, PasswordField, SubmitField | |
from wtforms.validators import DataRequired, Email, Length | |
class LoginForm(FlaskForm): | |
email = StringField(label='Email', validators=[DataRequired(), Email()]) | |
password = PasswordField(label='Password', validators=[DataRequired(), Length(8)]) | |
submit = SubmitField(label="Log in") | |
app = Flask(__name__) | |
app.secret_key = "test-secret" | |
@app.route("/") | |
def home(): | |
return render_template('index.html') | |
@app.route("/login", methods=["GET", "POST"]) | |
def login(): | |
login_form = LoginForm() | |
if login_form.validate_on_submit(): | |
email_reference = "[email protected]" | |
pw_reference = "12345678" | |
if (login_form.email.data == email_reference and login_form.password.data == pw_reference): | |
return render_template('success.html') | |
else: | |
return render_template('denied.html') | |
return render_template('login.html', form=login_form) | |
if __name__ == '__main__': | |
app.run(debug=True) | |
#login..html | |
... | |
<form method="POST" action="{{ url_for('login') }}" novalidate> | |
{{ form.csrf_token }} | |
<p> | |
{{ form.email.label }} <br> {{ form.email(size=30) }} | |
{% for err in form.email.errors %} | |
<span style="color:red">{{ err }}</span> | |
{% endfor %} | |
</p> | |
<p> | |
{{ form.password.label }} <br> {{ form.password(size=30) }} | |
{% for err in form.password.errors %} | |
<span style="color:red">{{ err }}</span> | |
{% endfor %} | |
</p> | |
{{ form.submit }} | |
</form> | |
... | |
# if combined with Bootstrap, it is possible to simplify the code in the HTML file | |
# https://pythonhosted.org/Flask-Bootstrap/basic-usage.html | |
{% extends 'bootstrap/base.html' %} | |
{% import "bootstrap/wtf.html" as wtf %} | |
... | |
{{ wtf.quick_form(form) }} # 'novalidate=True' as second parameter, to turn off the default validation check done by some browsers | |
... | |
# Form with dropdown list selection | |
# main.py | |
from wtforms import SelectField | |
... | |
class CafeForm(FlaskForm): | |
coffee_rating = SelectField("Coffee Rating", choices=["☕️", "☕☕", "☕☕☕", "☕☕☕☕", "☕☕☕☕☕"], validators=[DataRequired()]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment