Last active
March 19, 2018 20:37
-
-
Save cofinley/7651ea576f78e00f070401b52d2158b5 to your computer and use it in GitHub Desktop.
Flask with Forms
This file contains 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
from flask import Flask, render_template | |
from forms import RegistrationForm | |
app = Flask(__name__) | |
# Home page, just for showing routes with html files | |
@app.route('/home') | |
@app.route('/home/') # optional, just covers the following slash | |
@app.route('/home/<optional_subfolder>') # optional, just showing what you can do | |
def hello(optional_subfolder=None): # if parameter in route, you need to include parameter in function definition too | |
return render_template('home_page.html') # this html file goes in the folder "templates" in the project root folder | |
@app.route('/register', methods=['GET', 'POST']) # need GET to show form to user, POST to allow submission of form | |
def register(): | |
form = RegistrationForm() | |
if form.validate_on_submit(): # will be true if form validators' criteria met | |
# script call goes here to pass along form data | |
# form data will be in the form of form.username.data; it will be a string | |
# could import from your file | |
# import ryans_script | |
# ryans_script.function_name(form.email.data) | |
return redirect(url_for('hello')) # redirect to function name of first route (as a string), renders only when form is valid | |
return render_template('register.html', form=form) # fall back route that contains the form |
This file contains 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
from flask_wtf import FlaskForm | |
from wtforms import StringField, PasswordField | |
from wtforms.validators import DataRequired, Length, EqualTo | |
class RegistrationForm(FlaskForm): | |
username = StringField('Username', [Length(4,25), DataRequired()]) | |
email = StringField('Email Address', [Length(6,35), DataRequired()]) | |
password = PasswordField('New Password', [ | |
DataRequired(), | |
EqualTo('confirm', message='Passwords must match') | |
]) | |
confirm = PasswordField('Repeat Password') | |
accept_tos = BooleanField('I accept the TOS', [DataRequired()]) |
This file contains 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
<html> | |
<!-- simplified html --> | |
<body> | |
<form method="POST"> | |
{{ form.username.label }} {{ form.username(size=25) }} | |
<!-- repeat for all fields --> | |
<input type="submit" value="Go"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment