์ƒˆ์†Œ์‹

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

ํ”Œ๋ผ์Šคํฌ (Flask) - ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ (MVC ํŒจํ„ด )

  • -

*Flask ํšŒ์›๊ฐ€์ž…๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

 

 

*๊ตฌ์กฐ (Structure)

Flaskํด๋”
โ”‚   โ”œโ”€โ”€ templates (ํด๋”)
โ”‚          โ”œโ”€โ”€ register.html                                    
โ”‚          
โ”‚
โ”œโ”€โ”€ app.py (์‹คํ–‰ํŒŒ์ผ)                                           
โ”œโ”€โ”€ db.sqlite (์ด๊ฑด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋จ)
โ”œโ”€โ”€ models.py                  

 

* app.py                                  

from flask import Flask, render_template, request, redirect #render_template์œผ๋กœ htmlํŒŒ์ผ ๋ Œ๋”๋ง
from models import db
import os
from models import Fcuser
app = Flask(__name__)

@app.route('/')
def hello():
    return render_template("hello.html")
#GET = ํŽ˜์ด์ง€๊ฐ€ ๋‚˜์˜ค๋„๋ก ์š”์ฒญ. POST = ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ ์˜ค๋Š” ์š”์ฒญ/ ์š”์ฒญ์ •๋ณดํ™•์ธํ•˜๋ ค๋ฉด request ์ž„ํฌํŠธ ํ•„์š”
@app.route('/register', methods=['GET','POST'])
def register():
    if request.method == 'GET':
        return render_template("register.html")
    else:
        #ํšŒ์›์ •๋ณด ์ƒ์„ฑ
        userid = request.form.get('userid') 
        username = request.form.get('username')
        password = request.form.get('password')
        re_password = request.form.get('re_password')
        print(password) # ๋“ค์–ด์˜ค๋‚˜ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. 


        if not (userid and username and password and re_password) :
            return "๋ชจ๋‘ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"
        elif password != re_password:
            return "๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”"
        else: #๋ชจ๋‘ ์ž…๋ ฅ์ด ์ •์ƒ์ ์œผ๋กœ ๋˜์—ˆ๋‹ค๋ฉด ๋ฐ‘์—๋ช…๋ น์‹คํ–‰(DB์— ์ž…๋ ฅ๋จ)
            fcuser = Fcuser()         
            fcuser.password = password           #models์˜ FCuser ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด db์— ์ž…๋ ฅํ•œ๋‹ค.
            fcuser.userid = userid
            fcuser.username = username      
            db.session.add(fcuser)
            db.session.commit()
            return "ํšŒ์›๊ฐ€์ž… ์™„๋ฃŒ"

        return redirect('/')

if __name__ == "__main__":
    basedir = os.path.abspath(os.path.dirname(__file__))  # database ๊ฒฝ๋กœ๋ฅผ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์„ค์ •ํ•จ
    dbfile = os.path.join(basedir, 'db.sqlite') # ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„๊ณผ ๊ฒฝ๋กœ
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + dbfile
    app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True     # ์‚ฌ์šฉ์ž์—๊ฒŒ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ์ „๋‹ฌ์™„๋ฃŒํ–ˆ์„๋•Œ๊ฐ€ TEARDOWN, ๊ทธ ์ˆœ๊ฐ„๋งˆ๋‹ค COMMIT์„ ํ•˜๋„๋ก ํ•œ๋‹ค.๋ผ๋Š” ์„ค์ •
    #์—ฌ๋Ÿฌ๊ฐ€์ง€ ์Œ“์•„์ ธ์žˆ๋˜ ๋™์ž‘๋“ค์„ Commit์„ ํ•ด์ฃผ์–ด์•ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ˜์˜๋จ. ์ด๋Ÿฌํ•œ ๋‹จ์œ„๋“ค์€ ํŠธ๋ Œ์ ์…˜์ด๋ผ๊ณ ํ•จ.
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False   # Trueํ•˜๋ฉด warrnig๋ฉ”์‹œ์ง€ ์œ ๋ฐœ, 

    db.init_app(app) #์ดˆ๊ธฐํ™” ํ›„ db.app์— app์œผ๋กœ ๋ช…์‹œ์ ์œผ๋กœ ๋„ฃ์–ด์คŒ
    db.app = app
    db.create_all()   # ์ด ๋ช…๋ น์ด ์žˆ์–ด์•ผ ์ƒ์„ฑ๋จ. DB๊ฐ€


    app.run(host='127.0.0.1', port=5000, debug=True) 

return render_template('xxxx.html') - template ํด๋” ์•ˆ์˜ htmlํŒŒ์ผ์„ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค. 

@app.route('/register', methods=['GET','POST']) - GET ์™€ POST ๋‘๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ Method Not Allowed ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 ์ถ”๊ฐ€๋กœ POST, GET์€ ๋‘˜๋‹ค ์ •๋ณด๋ฅผ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•˜๊ธฐ์œ„ํ•œ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

์ฐจ์ด์ ์€ GET : ์ €์žฅ๋œ ์ •๋ณด๋ฅผ ๋‹จ์ˆœํžˆ ์š”์ฒญํ• ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, url์— ์ •๋ณด์˜ ์ด๋ฆ„์ด ๋…ธ์ถœ๋ฉ๋‹ˆ๋‹ค. 

POST : url์— ์ฟผ๋ฆฌ๊ฐ€ ๋…ธ์ถœ๋˜์ง€ ์•Š์œผ๋ฉฐ, ์š”์ฒญ์‹œ ๋ฐ์ดํ„ฐ ์–‘์— ์ œํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.

 

POST๋Š” ํ•œ๋งˆ๋””๋กœ ๋ฐ์ดํ„ฐ์— ์ˆ˜์ •์ด ์žˆ๋Š”๊ฒฝ์šฐ (๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ, ํšŒ์›๊ฐ€์ž… ์ •๋ณด ๋ฐ›๊ธฐ)

GET์€ ๋‹จ์ˆœํžˆ ์ •๋ณด๋ฅผ ๋ณด๊ธฐ์œ„ํ•œ ์šฉ๋„? (๊ฒŒ์‹œ๊ธ€ ์ฝ๊ธฐ, ํšŒ์›๊ฐ€์ž…์ฐฝ ๋ณด๊ธฐ)

*register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</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 mt-5">
            <h1>ํšŒ์›๊ฐ€์ž…</h1>
        </div>
        <div class="row mt-5">
        <div class="col-12">
            <form method="POST" >
                <div class="form-group">
                    <label for="userid">์•„์ด๋””</label>
                    <input type="text" class="form-control" id="userid" placeholder="์•„์ด๋””" name="userid"/>
                    <!-- id๋Š” ์‚ญ์ œํ•ด๋„ ๋˜๋”๋ผ. ๋งˆ์ง€๋ง‰์— name๊ณผ app.py์˜ get ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์ด ์ผ์น˜ํ•ด์•ผ ์ƒ์‘ํ•œ๋‹ค -->
                </div>
                <div class="form-group">
                    <label for="username">์‚ฌ์šฉ์ž ์ด๋ฆ„</label>
                    <input type="text" class="form-control" id="username" placeholder="์‚ฌ์šฉ์ž ์ด๋ฆ„" name="username"/>

                </div>
                <div class="form-group">
                    <label for="password">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
                    <input type="password" class="form-control" id="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" name="password"/>
                </div>
                <div class="form-group">
                    <label for="re_password">๋น„๋ฐ€๋ฒˆํ˜ธํ™•์ธ</label>
                    <input type="password" class="form-control" id="re_password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธํ™•์ธ" name="re_password"/>
                </div>
                <button type="submit" class="btn btn-primary">๋“ฑ๋ก</button>
            </form>
        </div>
        </div>
</body>
</html>

form์€ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ(bootstrap)์„ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

<form method="POST" >: input์„ ๊ฐ์‹ธ๋Š” ํƒœ๊ทธ์— ๋ฉ”์†Œ๋“œ๋ฅผ ํ‘œ์‹œํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<input type="text" class="form-control" id="userid" placeholder="์•„์ด๋””" name="userid"/>

 

 

*Models.py (๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค)

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()           #SQLAlchemy๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ €์žฅ

class Fcuser(db.Model): 
    __tablename__ = 'fcuser'   #ํ…Œ์ด๋ธ” ์ด๋ฆ„ : fcuser
    id = db.Column(db.Integer, primary_key = True)   #id๋ฅผ ํ”„๋ผ์ด๋จธ๋ฆฌํ‚ค๋กœ ์„ค์ •
    password = db.Column(db.String(64))     #ํŒจ์Šค์›Œ๋“œ๋ฅผ ๋ฐ›์•„์˜ฌ ๋ฌธ์ž์—ด๊ธธ์ด 
    userid = db.Column(db.String(32))       #์ดํ•˜ ์œ„์™€ ๋™์ผ
    username = db.Column(db.String(8))

 

์—ฌ๊ธฐ๊นŒ์ง€ ๋งŒ๋“ค๊ณ  app.py๋ฅผ ํŒŒ์ด์ฌ์ƒ์—์„œ ์‹คํ–‰์‹œํ‚ค๋ฉด

์—ฌ๊ธฐ๊นŒ์ง€ํ•˜๋ฉด ์™„์„ฑ์ž…๋‹ˆ๋‹ค. ์•„์ด๋””, ์ด๋ฆ„, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•œ ํ›„, ์ œ์ถœ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด db.sqlite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.

*DB๋กœ ์ž˜ ๋„˜์–ด๊ฐ€๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

(๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” input type='password' ๋ผ์„œ ๋™๊ทธ๋ผ๋ฏธ๋กœ ํ‘œ์‹œ๋˜์—ˆ์œผ๋‚˜, 1234๋กœ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.)

์ œ์ถœ์„ ๋ˆ„๋ฅธ๋’ค db.sqlite๋ฅผ ์—ด์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ž˜ ๋“ค์–ด๊ฐ„ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

Contents

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

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