์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

Flask - ๊ฐ„๋‹จ ์˜ˆ์ œ : ๋ง์…ˆ ๋ฌธ์ œ ์ œ์ถœ + ์ฒด์  ํ•˜๊ธฐ (HTTP ์š”์ฒญ๊ณผ ์‘๋‹ต)

  • -

 

 

 

HTTP์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋กœ ํ’€์–ด๋ณด์•˜๋‹ค.

from flask import request get_number = request.form.get('get_number')
  • HTTP์˜ Method (GET POST)๋ฅผ ํ†ตํ•ด ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์€ request๋ผ๋Š” ์ „์—ญ๋ณ€์ˆ˜์— ์ €์žฅ๋œ๋‹ค.
  • ์ด๋•Œ request.form.get('html์˜ name์†์„ฑ') ์„ ํ†ตํ•ด ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 
  • request๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด import๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

ํŒŒ์ด์ฌ์—์„œ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•œ๋’ค, ๋‹ค์‹œ templates์œผ๋กœ ๋ณด๋‚ผ๋•Œ๋Š” 

return render_template('index.html' , number1=number1)

์ด๋Ÿฌํ•œ ํ˜•์‹์œผ๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด number1์ด๋ผ๋Š” ๋ณ€์ˆ˜๋Š” HTML์—์„œ {{number1}} ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

*app.py
from flask import Flask, render_template, request from random import randint app = Flask(__name__) correct = [] i = 0 o_cnt, x_cnt = 0,0 @app.route('/', methods=["POST","GET"]) def postTest(): number1,number2 = randint(0,10),randint(0,100) msg = "๋ฌธ์ œ ์‹œ์ž‘" global correct , i , o_cnt , x_cnt correct.append(number1+number2) get_number = request.form.get('get_number') if request.method == "POST": if int(get_number) == correct[i]: msg = "์ •๋‹ต์ž…๋‹ˆ๋‹ค." o_cnt += 1 else : x_cnt -= 1 msg = "๋•ก ! ์ •๋‹ต์€ %d์ž…๋‹ˆ๋‹ค."%correct[i] i+=1 return render_template('index.html' , number1=number1 ,number2=number2 ,msg=msg ,o_cnt = o_cnt ,x_cnt = x_cnt) if __name__ == "__main__" : app.run(host = '127.0.0.1', port = 5000 , debug=True)

 

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>๊ฐ„๋‹จ์˜ˆ์ œ</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> </head> <body> <div class="container"> <div class="row"> <div class="col"> <h1>Flask_simle example</h1><br><br><br> </div></div></div> <div class="container"> <div class="row"> <div class="col"> <form method="POST"> <h2>Q. {{number1}} + {{number2}} = ?</h2> <input type="text" name="get_number"> <button type="submit" class="btn btn-primary">์ œ์ถœ</button> <br><br><br> {% if msg %} <script> alert("{{msg}}")</script> {% endif %} <div class="alert alert-primary" role="alert"> ๋งž์€ ๋ฌธ์ œ : {{o_cnt}} </div> <div class="alert alert-danger" role="alert"> ํ‹€๋ฆฐ ๋ฌธ์ œ : {{x_cnt}} </div> </form></div></div></div> </body> </html>
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.