Flask(3)

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

Eager to learn Flask ๐Ÿ”ฅ

๋ชฉ๋ก ๋ณด๊ธฐ
3/7

ํผ์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก๋ฐ›๊ธฐ

URI Path param
URI Query param
Body json
Form data + file
Header
  • ๋ฐ์ดํ„ฐ ์ „์†ก๋ฐ›์„ ๋•Œ form์€ ์ข€ ๋‚ฏ์„ค์—ˆ๋Š”๋ฐ flask ๊ณต๋ถ€ํ•˜๋Š” ์ฐธ์— form์— ๋Œ€ํ•ด์„œ๋„ ์•Œ์•„๋ณด์žฅ!
    ๊ธฐ๋ณธ์ ์œผ๋กœ Json ์ „์— form์œผ๋กœ ์ผ๋‹ค๊ณ  ์•Œ๊ณ  ์žˆ๋‹ค. json์€ ํŒŒ์ผ(์ด๋ฏธ์ง€) ์ฒจ๋ถ€ ๊นŒ๋‹ค๋กœ์›Œ์„œ ์ด๋ฏธ์ง€ ๊ฐ™์€ ํŒŒ์ผ ๋งŒํผ์€ ์˜ˆ์™ธ๋กœ ํผ๋ฐ์ดํ„ฐ๋กœ ๋ฐ›๋Š”๋‹ค๊ณ  ํ•œ๋‹ค.

Form

# app.py

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/')
def student():
    return render_template('student.html')

@app.route('/result', methods = ['POST', 'GET'])
def result():
    if request.method == 'POST':
        result = request.form
        return render_template("result.html", result=result)

if __name__ == '__main__':
    app.run(debug = True)
  • result = request.form
    ํผ ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ถ€๋ถ„์€ ์ด ์ฝ”๋“œ์ด๋‹ค. ์š”์ฒญ์ด POST์ผ ๊ฒฝ์šฐ, reqeust์˜ form ๊ฐ์ฒด์— ํ•ด๋‹น ํผ์— ์ž…๋ ฅ๋œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. ๋งŒ์•ฝ ํŠน์ • ํ•„๋“œ ๊ฐ’๋งŒ ๋ฝ‘์•„๋‚ด๊ณ  ์‹ถ๋‹ค๋ฉด request.form['name']์™€ ๊ฐ™์€ ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.

  • ํผ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ณด๊ด€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” items() ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

submit ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋œ ํ‘œ๋ฅผ ํ™•์ธํ•˜๋Š” ํŽ˜์ด์ง€(result.html)๋กœ ์ด๋™ํ•œ๋‹ค!!!๊ณ  ํ•˜๋Š”๋ฐ ์™œ ์ด๋™์•ˆ๋˜๋ƒ ^^... ์–ด๋””๊ฐ€ ํ‹€๋ ธ์ง€.. ๋‹ค์‹œ ํ•ด๋ด์•ผ๊ฒŒ๋”ฐ...^^

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

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