6 ms·
You'd use Flask-WTF for forms. https://flask-wtf.readthedocs.org/en/latest/ https://flask-wtf.readthedocs.org/en/latest/ It can also handle file uploads. htt
by glimcat 12y ago
You'd use Flask-WTF for forms.
https://flask-wtf.readthedocs.org/en/latest/ https://flask-wtf.readthedocs.org/en/latest/
It can also handle file uploads.
https://flask-wtf.readthedocs.org/en/latest/form.html https://flask-wtf.readthedocs.org/en/latest/form.html
You'd need to define a model which describes the form fields, handle it in your view, and add it to your page template.
forms.py
from flask_wtf import Form
from wtforms import TextField, PasswordField
from wtforms.validators import DataRequired
class LoginForm(Form):
email = TextField('Email', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
views.py
from forms import LoginForm
@app.route('/login', methods=('GET', 'POST'))
def login():
form = LoginForm()
if form.validate_on_submit():
email = form.data.get('email', None)
password = form.data.get('password', None)
do_login_stuff(email, password)
else:
return render_template('login_page.html', form=form)
login_page.html
{% extends "base.html" %}
{% block content %}
<h3>Log in:</h3>
<form method="POST" action="{{ url_for('login') }}">
{{ form.hidden_tag() }}
{{ form.email.label }} {{ form.email }}
{{ form.password.label }} {{ form.password }}
<input type="submit" value="Log in">
</form>
{% endblock %}
- hackerews 12y agoYeah that's the tough part. I think I'd have to set up a model for each script I add to flask.
- glimcat 12y agoHere's an option for organizing a "bunch of scripts" app that might be less intimidating to maintain. foo_script.py from flask import render_template from flask_wtf import Form from wtforms import TextField from wtforms.validators import DataRequired class ScriptForm(Form): param1 = TextField('Param1', validators=[DataRequired()]) param2 = TextField('Param2', validators=[DataRequired()]) param3 = TextField('Param3', validators=[DataRequired()]) @app.route('/foo-script', methods=('GET', 'POST')) def foo_script(): form = ScriptForm() if form.validate_on_submit(): do_stuff(form.data) else: return render_template('foo_script.html', form=form) def do_stuff(data): for key in data: print '%s: %s' % (key, data[key]) foo_script.html {% extends "base.html" %} {% block content %} <h3>Run foo script:</h3> <form method="POST" action="{{ url_for('foo_script') }}"> {{ form.hidden_tag() }} {{ form.param1.label }} {{ form.param1 }} {{ form.param2.label }} {{ form.param2 }} {{ form.param3.label }} {{ form.param3 }} <input type="submit" value="Run foo script"> </form> {% endblock %} base.html <!DOCTYPE html> <html lang="en"> <head> <title>FooCorp Script Service</title> </head> <body> {% block content %}{% endblock %} </body> </html> app.py from flask import Flask app = Flask(__name__) app.config.from_pyfile('config.py') import foo_script, bar_script, baz_script config.py # You need this for CSRF protection & cookie signing SECRET_KEY = randomly_generated_secret_key You should also look into Flask's blueprints at some point if it keeps growing. But it's not really essential, just another tool to help you keep projects organized. Flask is mostly "do whatever makes sense in your specific case" rather than imposing many global constraints on structure. Also, I'd use gunicorn if you're deploying your own server. It's a bit less intimidating to get set up than uWSGI, and the main tradeoff is that it doesn't support quite as many thousands of users (i.e. not relevant).