Flask (2)

majaeh43ยท2021๋…„ 12์›” 8์ผ
0

Eager to learn Flask ๐Ÿ”ฅ

๋ชฉ๋ก ๋ณด๊ธฐ
2/7
  • Flask์—์„œ html ๋ฌธ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

Templates

ํ”Œ๋ผ์Šคํฌ์˜ ๊ฒฝ์šฐ, HTML ๋ฌธ์„œ๋ฅผ ๋ณด๊ด€ํ•˜๊ธฐ ์œ„ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ ํ•ด๋‹น ํŒŒ์ผ๋“ค์„ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•ด๋‹น ํด๋”์˜ ์œ„์น˜๋Š” ๋ฐ˜๋“œ์‹œ ํ”Œ๋ผ์Šคํฌ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ์ฝ”๋“œ์™€ ๊ฐ™์€ ๊ฒฝ๋กœ์ด๋ฉฐ, templates๋ผ๋Š” ์ด๋ฆ„์ด์—ฌ์•ผ ํ•œ๋‹ค. ๋จผ์ € template ํด๋” ๋‚ด์— hello.html ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ.

# hello.html

<!doctype html>
<html>
   <body>
   
      <h1>Hello {{ name }}!</h1>
      
   </body>
</html>

์ด์ œ, app.py๋ฅผ ์ˆ˜์ •์„ ์ชผ๊ธˆ ํ•ด์ฃผ๋ฉด!

# app.py

from flask import Flask, render_template
# render_template import ํ•ด์ฃผ๊ณ 

app = Flask(__name__)

@app.route('/hello/<user>')
def hello_name(user):
   return render_template('hello.html', name = user)
# importํ•ด์ค€ render_template๋ฅผ return ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฅฌ๊ธฐ

if __name__ == '__main__':
   app.run(debug = True)
# ์•ž์„  ๋ธ”๋กœ๊น…์—์„œ ์–ธ๊ธ‰ํ–ˆ๋Š”๋ฐ, debug ์˜ต์…˜์„ True๋กœ ๋ณด๋‚ด์ฃผ๋ฉด, ์›น ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰ ์ค‘์— ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด๋„ ํ•ด๋‹น ์ž‘์—…์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜์˜์‹œํ‚จ๋‹ค. 
# ์‹ค์ œ๋กœ ๊ฐœ๋ฐœ ์ค‘์— ๊ตฌํ˜„ ํ˜„ํ™ฉ์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด ์—„์ฒญ ํŽธ๋ฆฌํ•˜๋‹ค. ์•„๋ฌด ์ธ์ž๋„ ์ฃผ์ง€ ์•Š์„ ๊ฒฝ์šฐ, False ์ธ์ž๋กœ ๋ณด๋‚ด์ง„๋‹ค.
  • render_template('hello.html', name = user)
    ํ”Œ๋ผ์Šคํฌ์—์„œ ์™ธ๋ถ€์˜ html ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฐ”๋กœ ์ด๊ฒƒ์ด๋‹ค. templates ํด๋”์—์„œ ์ธ์ž๋กœ ๋ฐ›์€ hello.html ํŒŒ์ผ์„ ํƒ์ƒ‰ํ•œ ํ›„, name ์ธ์ž๋ฅผ ๊ฑด๋„ค์ฃผ๋ฉฐ ํŽ˜์ด์ง€๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.

  • Hello {{ name }}!
    ์ธ์ž๋กœ ๋ฐ›์€ name ๋ณ€์ˆ˜๋Š” ์ด๋Ÿฐ ํ˜•ํƒœ๋กœ html ๋ฌธ์„œ์—์„œ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ { } ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ html ๋ฌธ์„œ ๋‚ด์—์„œ ์ง์ ‘ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ๋ฌธ๋ฒ•์„ Template Engine์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ์ด ํ…œํ”Œ๋ฆฟ ์—”์ง„์˜ ์ด๋ฆ„์€ Jinja2๋ผ๊ณ  ํ•œ๋‹ค.

  • Jinja2
    ์ด ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•˜์—ฌ HTML ๋ฌธ์„œ์—์„œ ๋ช‡ ๊ฐ€์ง€ ํŒŒ์ด์ฌ ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. (if ๋ฌธ, for ๋ฌธ ๋ฐ ์ผ๋ถ€ ๋ฉ”์†Œ๋“œ๋“ค ์‚ฌ์šฉ๊ฐ€๋Šฅ) ์ฝ”๋“œ ์šฉ๋„์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๊ด„ํ˜ธ๋ฅผ ๊ฐ์‹ธ์ฃผ์–ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

>> ์š”๋ ‡๊ฒŒ!
{% ... %} for Statements
{{ ... }} for Expressions to print to the template output
{# ... #} for Comments not included in the template output
# ... ## for Line Statements

locals() ๋ฉ”์†Œ๋“œ ํ™œ์šฉํ•˜๊ธฐ

์›น ํŽ˜์ด์ง€์˜ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ HTML ๋ฌธ์„œ์—์„œ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•  ๋ณ€์ˆ˜๋‚˜ ์ž๋ฃŒํ˜•์ด ๋งค์šฐ ๋งŽ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•ด๋‹น ํ•จ์ˆ˜ ๋‚ด์˜ ์ง€์—ญ ๋ณ€์ˆ˜ ์ „์ฒด๋ฅผ ์ธ์ž๋กœ ๋ณด๋‚ด์ฃผ๋Š” locals() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋งค์šฐ ํŽธ๋ฆฌํ•˜๋‹ค.

# app.py

from flask import Flask, flash, redirect, render_template, request
 
app = Flask(__name__)
 
@app.route("/")
def index():
    return "Flask App!"
 
@app.route("/user/")
def hello():
 
    users = [ "Frank", "Steve", "Alice", "Bruce" ]
    var = 1
    return render_template('user.html', **locals())
 
if __name__ == "__main__":
    app.run(host='0.0.0.0',debug = True)
  • locals() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ ์ง€์—ญ ๋‚ด์—์„œ ์„ ์–ธ๋œ users ๋ฆฌ์ŠคํŠธ์™€ var ๋ณ€์ˆ˜๋ฅผ ๋ชจ๋‘ render_template ํ•จ์ˆ˜ ๋‚ด์— ๋ณด๋‚ด์ค€๋‹ค.

์‹ ๊ธฐํ•ด... ํ›„...!

๐Ÿ˜œ Flask๋ฅผ ํ†ตํ•ด ์›น ๊ตฌ๋™ํ•  ๋•Œ, ๊ฐ URL ๊ฐ„์˜ ์ด๋™(Redirect) ๋ฐ HTTP ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€! ๐Ÿ˜œ

Redirect(๋ฆฌ๋‹ค์ด๋ ‰ํŠธ)

์žฅ๊ณ ์—์„œ๋Š” urls.py๋ผ๊ณ  ํ•ด์„œ ๋ถ€๋ชจ urls.py ์ž์‹ urls.py๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค๋ฉด, Flask์—์„œ๋Š” URL ๊ฐ„์˜ ์‰ฝ๊ณ  ํŽธํ•œ ์ด๋™์„ ์œ„ํ•ด redirect ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค!

# app3.py

from flask import Flask, redirect, url_for
app = Flask(__name__)

@app.route('/admin')
def hello_admin():
    return 'Hello Admin'

@app.route('/guest/<guest>')
def hello_guest(guest):
    return 'Hello %s as Guest' % guest

@app.route('/user/<name>')
def hello_user(nmae):
    if name == 'admin':
        return redirect('/admin')
    else:
        return redirect(url_for('hello_guest', guest=name))
        
if __name__ == '__main__':
    app.run(debug=True)
  • ์œ„ ์ฝ”๋“œ๋Š” /user/ URL์— ์ž…๋ ฅ๋œ name ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ URL๋กœ ํ–ฅํ•˜๊ฒŒ ๋˜์–ด ์žˆ์Œ! name ๊ฐ’์ด admin์ผ ๊ฒฝ์šฐ, /admin URL(hello_admin())๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ , ๋‚˜๋จธ์ง€๋Š” guest/ URL(hello_guest)๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ตฌ์กฐ์ด๋‹ค.

  • return redirect('/admin') == return redirect(url_for("hello_admin"))
    ํ•ด๋‹น URL๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ด๋‹ค. URL์„ ์ค˜๋„ ๋˜๊ณ , url_for() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹ค์ œ ํ•จ์ˆ˜ ์ด๋ฆ„์„ ์ธ์ž๋กœ ์ฃผ์–ด๋„ ๋จ!! ๐Ÿ˜Ž ๋‚˜๋Š” ์ „์ž๊ฐ€ ๋” ํŽธํ•œ ๊ฒƒ ๊ฐ™๋‹ค!

  • return redirect(url_for('hello_guest', guest = name))
    url_for์˜ ๊ฒฝ์šฐ, ๋‚˜๋จธ์ง€ ์ธ์ž๋ฅผ ๋„˜๊ฒจ ์ค„๋•Œ, ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ณ€์ˆ˜๋กœ ๋‚˜๋ˆ ์„œ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์ด์ฌ์€ ๋ฌธ์ž์—ด ์ ‘๊ทผ์ด ์›Œ๋‚™ ํŽธํ•˜๋‹ˆ๊นŒ ์‹ค์ œ URL๋กœ ์ค˜๋„ ๋น„์Šทํ•˜๋‹ˆ, ํŽธํ•œ๊ฑธ ์“ฐ๊ธฐ~~ ๐Ÿ˜Ž

HTTP Methods

from flask import Flask, redirect, url_for, request
app = Flask(__name__)

@app.route('/success/<name>')
def success(name):
    return 'welcome %s' % name

@app.route('/login', methods = ['POST', 'GET'])
def login():
    if request.method == 'POST':
        user = request.form['nm']
        return redirect(url_for('success', name=user))
    else:
        user = request.args.get('nm')
        return redirect(url_for('success', name=user))
        
if __name__ == '__main__':
    app.run(debug = True)
  • @app.route('/login',methods = ['POST', 'GET'])
    ๊ธฐ๋ณธ์ ์œผ๋กœ Flask์˜ ๋ผ์šฐํŒ…์€ GET Method๋กœ ์‘๋‹ตํ•˜์ง€๋งŒ, ํ•„์š”ํ•œ ๊ฒฝ์šฐ ํŠน์ • ๋ฉ”์†Œ๋“œ์—๋งŒ ์‘๋‹ตํ•˜๋„๋ก ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ POST ๋ฐ GET 2๊ฐœ์˜ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ์‘๋‹ต์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜์˜€๋‹ค.
    ๋‚˜๋ˆ ์„œ ๋„ฃ์–ด์ค˜๋„ ๋จ!

  • if request.method == 'POST':
    POST ๋ฉ”์†Œ๋“œ๋กœ ์š”์ฒญ์ด ์™”์„ ๋•Œ.

  • user = request.form['nm']
    ์ด ์ฝ”๋“œ๋Š” ํ•ด๋‹น ์š”์ฒญ์—์„œ ์ž…๋ ฅ๋œ form ์ •๋ณด์˜ 'nm' ํ•„๋“œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฒ ๋‹ค๋Š” ๋œป. ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ง์ ‘ form์„ ๋งŒ๋“ค์–ด์„œ ์‹คํ–‰์‹œ์ผœ์•ผ ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ html ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜์—ฌ ๊ทธ๋Œ€๋กœ ์—ด์–ด๋ณด์ž.

# asd.html
  
<html>
   <body>
      
      <form action = "http://localhost:5000/login" method = "post">
         <p>Enter Name:</p>
         <p><input type = "text" name = "nm" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
      
   </body>
</html>
  • ์›น ์„œ๋ฒ„์— ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ง์ ‘ ํŒŒ์ผ์„ ์—ด์–ด์•ผ ํ•œ๋‹ค. ํ•ด๋‹น ํผ์ด ๋‹ด๊ธด html ๋ฌธ์„œ๋Š” ๋”ฑํžˆ flask์™€ ์—ฐ๊ฒฐ๋œ ๊ฒŒ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ. ๊ทธ๋Ÿฌ๋‚˜ form์˜ action ํ•„๋“œ์— ํ•ด๋‹น URL๋กœ ํ•ด๋‘๋ฉด POST ์š”์ฒญ์„ ์›น ์„œ๋ฒ„๋กœ ๋ณด๋‚ด์„œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

  • else: user = request.args.get('nm')
    else ๋ฌธ์—๋Š” ๋‹น์—ฐํžˆ ๋‚จ์€ GET ๋ฉ”์†Œ๋“œ ์š”์ฒญ์ด ์˜ค๊ฒŒ ๋œ๋‹ค. get('nm')๋ฅผ ํ†ตํ•ด URL์„ ํ†ตํ•ด ์ „๋‹ฌ๋ฐ›์€ nm ํ•„๋“œ์˜ ์ธ์ž๋ฅผ Flask๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฒˆ์—” ๋”ฐ๋กœ form์„ ๋งŒ๋“ค ํ•„์š” ์—†์ด ๋ฐ”๋กœ URL๋กœ ์ธ์ž๋ฅผ ์ฃผ๋ฉด ๋œ๋‹ค.

http://127.0.0.1:5000/login?nm=eunhye

์ถœ์ฒ˜: https://blog.naver.com/shino1025/221348321746
์ถœ์ฒ˜: https://blog.naver.com/shino1025/221348289584

profile
๊ฐœ๋ฐœ ๊ธฐ๋ก

0๊ฐœ์˜ ๋Œ“๊ธ€