[PYTHON] Flask's personal note # 1

Write notes to use Flask for the first time in business.

Use of modules

from flask import Flask,render_template,request

#from module import class 1,Class 2,Class 3...

How to receive a GET request

#Use request class
from flask import Flask, render_template, request

name = request.args.get("name")

How to receive a POST request

#Use request class and prepare method when received by POST
from flask import Flask, render_template, request

@app.route("/post", methods = ["post"])
def post():
    name = request.form["name"]
    #For lists, use getlist
    names = request.form.getlist("names")

Variable rendering

#Controller side
 render_template("index.html", name = name)

#Template side
<div>{{name}}</div>

Use if statement in template

#Template side
{% if name == "tokyo" %}
<div>capital{{name}}</div>
{% elif name %}
<div>{{name}}</div>
{% else %}
<div>Anonymous</div>
{% endif %}

Use for statement in template

#Template side
{% for city in cities %}
<div>{{city}}</div>
{% endfor %}

routing

# GET
@app.route("/get")
def get():

# POST
@app.route("/post", methods = ["post"])
def post():

Install modules together

pip install -r requirements.txt

The contents of requirements.txt look like this

Flask==1.1.1
SQLAlchemy==1.3.10
・
・
・
Add more modules you want to install

Continue to Part 2

Recommended Posts

Flask's personal note # 2
Flask's personal note # 1
Completely personal note
(Personal note) Sankey diagram
Note
Note
[AtCoder] ABC165C Personal Note [Python]
Django note 4
pyenv note
Program for Twitter Trend Analysis (Personal Note)
Deploy Django api on heroku (personal note)
Note: Python
[Personal note] Web page scraping with python3
Python note
Django Note 1
Django note 3
[Note] RepresenterError