LoginSignup
1
0

More than 1 year has passed since last update.

今日はFlaskを学びました。久々のPython、忘れていました。

1. HTMLだけのシンプルなWebページの作成

index.html
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
    <h1>タイトル</h1>
    </body>
</html>
app.py
from flask import Flask,render_template

app=Flask(__name__)

@app.route("/")
def hello():
    return"Hello World"

@app.route("/index")
def index():
    return render_template("index.html")

if __name__=="__main_":
    app.run(debug=True)

2.Webページに画像を表示

index.html
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
    <h1>Christmas</h1>
    <img src="/static/images/xm01.jpg" alt="クリスマス">
    </body>
</html>

3.クエリストリングを受け取ってhtmlに送る

app.py
from flask import Flask,render_template,request

app=Flask(__name__)

@app.route("/")
@app.route("/index")
def index():
    name=request.args.get("name")
    return render_template("idnex.html",name=name)

if __name__=="__main__":
    app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>{{name}}</title>
    </head>
    <body>
    <h1>Christmas</h1>
    <img src="/static/images/xm01.jpg" alt="クリスマス">
    </body>
</html>

4.html内でif文を書く

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>{{name}}</title>
    </head>
    <body>
    {% if name == "white" %}
    <h1>Happy {{name}} Christmas♪</h1>
    <img src="/static/images/xm01.jpg" alt="クリスマス1">
    {% elif name%}
    <h2>{{name}} Christmas</h2>
    <img src="/static/images/xm02.jpg" alt="クリスマス2">
    {% else %}
    <h3>Christmas</h3>
    <img src="/static/images/xm03.jpg" alt="クリスマス3">
    {% ebdif %}
    </body>
</html>

5.html内でfor文を書く

app.py
from flask import Flask,render_template,request

app=Flask(__name__)

@app.route("/")
@app.route("/index")
def index():
    name=request.args.get("name")
    song=["O Tannenbaum, o Tannenbaum,","Wie treu sind deine Blätter!","Du grünst nicht nur zur Sommerzeit,","Nein auch im Winter wenn es schneit.","O Tannenbaum, o Tannenbaum,","Wie treu sind deine Blätter!"]
    return render_template("idnex.html",name=name,song=song)

if __name__=="__main__":
    app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>{{name}}</title>
    </head>
    <body>
    {% if name == "white" %}
    <h1>Happy {{name}} Christmas♪</h1>
    <img src="/static/images/xm01.jpg" alt="クリスマス1">
    {% elif name%}
    <h2>{{name}} Christmas</h2>
    <img src="/static/images/xm02.jpg" alt="クリスマス2">
    {% else %}
    <h3>Christmas</h3>
    <img src="/static/images/xm03.jpg" alt="クリスマス3">
    {% ebdif %}
    {% for word in song %}
    <p>{{song}}</p>
    {% ebdfor %}
    </body>
</html>

6.POSTリクエストを受け取る

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>{{name}}</title>
    </head>
    <body>
    {% if name == "white" %}
    <h1>Happy {{name}} Christmas♪</h1>
    <img src="/static/images/xm01.jpg" alt="クリスマス1">
    {% elif name%}
    <h2>{{name}} Christmas</h2>
    <img src="/static/images/xm02.jpg" alt="クリスマス2">
    {% else %}
    <h3>Christmas</h3>
    <img src="/static/images/xm03.jpg" alt="クリスマス3">
    {% ebdif %}
    <form action="/index" method="POST">
    <input type="text" name="name" placeholder="Enter name">
    <input type="submit" placeholder="Submit">
    </form>
    {% for word in song %}
    <p>{{song}}</p>
    {% ebdfor %}
    </body>
</html>
app.py
from flask import Flask,render_template,request

app=Flask(__name__)

@app.route("/")
@app.route("/index")
def index():
    name=request.args.get("name")
    song=["O Tannenbaum, o Tannenbaum,","Wie treu sind deine Blätter!","Du grünst nicht nur zur Sommerzeit,","Nein auch im Winter wenn es schneit.","O Tannenbaum, o Tannenbaum,","Wie treu sind deine Blätter!"]
    return render_template("idnex.html",name=name,song=song)
@app.route("/index",methods=["post"])
def post():
    name=request.form("name")
    song=["Dashing through the snow, in a one-horse open sleigh,",
"O'er the fields we go, laughing all the way.","Bells on bob-tails ring, making spirits bright,","What fun it is to ride and sing a sleighing song tonight."]
    return render_template("idnex.html",name=name,song=song)


if __name__=="__main__":
    app.run(debug=True)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0