[PYTHON] Minimum knowledge to use Form in Flask

Overview

How to define, render and validate a Form using Flask's Flask-WTF

Form definition

class AnimalForm(FlaskForm):
    name = StringField('name', validators=[validators.Required(), validators.length(max=30)])
    kind = StringField('kind', validators=[validators.Required(), validators.length(max=10)])
    description = TextAreaField('description', validators=[])
point

--Create by inheriting FlaskForm --Multiple validators can be passed to validators

Form rendering

      <form method="POST">
      <p>{{ form.name.label }}:{{ form.name(size=30) }}</p>
      <p>{{ form.kind.label }}:{{ form.kind(size=20) }}</p>
      <p>{{ form.description.label }}:{{ form.description(cols="50", rows="20") }}</p>
      <button type="submit">Submit</button>
      </form>
point

--You can get the name defined in form by accessing label --Each field is callable and renders HTML --When rendering a field, if you pass an argument, it will be converted to an HTML property.

Validation of Submitted Form

    form = AnimalForm()
    if form.validate_on_submit():
        name = form.name.data
        kind = form.kind.data
        description = form.description.data
point

--valid_on_submit checks whether it is a post and validates the data --You can get the validated data by accessing data

Recommended Posts

Minimum knowledge to use Form in Flask
Easy to use Flask
How to use Golang flag package (minimum knowledge)
How to use classes in Theano
Mock in python-how to use mox
How to use SQLite in Python
Use Flask to run external files
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use calculated columns in CASTable
[Introduction to Python] How to use class in Python?
How to use Google Test in C
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use regular expressions in Python
How to use Map in Android ViewPager
How to use is and == in Python
Tips when amateurs use Flask in Visual Studio
How to use the C library in Python
Try to separate Controllers using Blueprint in Flask
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
EP 11 Use `zip` to Process Iterators in Parallel
Use cryptography module to handle OpenSSL in Python
How to use tkinter with python in pyenv
Use ELMo, BERT, USE to detect anomalies in sentences
Use pygogo to get the log in json.
Date of Address already in use error in Flask
Error in Flask OSError: [Errno 98] Address already in use
Use os.getenv to get environment variables in Python
[For beginners] How to use say command in python!
The easiest way to set up Last-Modified in Flask
I want to transition with a button in flask
A memorandum on how to use keras.preprocessing.image in Keras
Set the form DateField to type = date in Django
How to use bootstrap in Django generic class view
How to use template engine in pyramid 1 file application
How to use the exists clause in Django's queryset
Reasons to use long type in SQLite3 (C # Mono.Data.Sqlite)
How to use variables in systemd Unit definition files
How to change static directory from default in Flask
Convenient to use matplotlib subplots in a for statement
I tried to summarize how to use pandas in python
How to use jupyter lab in Windows 10 local environment
How to use the model learned in Lobe in Python
How to use Decorator in Django and how to make it
I was addicted to trying logging.getLogger in Flask 1.1.x
Use date to x-axis of tsplot depicted in seaborn
How to use Spacy Japanese model in Google Colaboratory
I want to use the R dataset in python
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
Use config.ini in Python
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
Use DataFrame in Java