๐Ÿ“ ๋ Œ๋”๋ง ํ…œํ”Œ๋ฆฟ

๋ Œ๋”๋ง(Rendering) : ๋งˆํฌ์—… ์–ธ์–ด๋ฅผ ๋ธŒ๋ผ์šฐ์ €(Client)์— ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ๊ณผ์ •

ํ”Œ๋ผ์Šคํฌ ๋‚ด์—์„œ html ํŒŒ์ผ์„ ์‚ฌ์šฉํ•ด ์›น ์‚ฌ์ดํŠธ๋ฅผ ์ œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ Œ๋”๋ง ํ…œํ”Œ๋ฆฟ์ด๋‹ค.
html ์ฝ”๋“œ๋“ค์„ ํ”Œ๋ผ์Šคํฌ๋‚ด์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํŒŒ์ผ์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ๋ผ์šฐํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์œ ์ง€๋ณด์ˆ˜์—์„œ ์ด๋กญ๋‹ค.

ํ”Œ๋ผ์Šคํฌ๋Š” render_template( )์„ ์ด์šฉํ•ด html ํŒŒ์ผ์„ ๋ Œ๋”๋งํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์— ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

from flask import render_template

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
    return render_template('hello.html', name=name)

hello ํ•จ์ˆ˜์˜ URL์— ์ ‘๊ทผํ•  ๋•Œ, hello.html์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ํ•ด๋‹น html ํŒŒ์ผ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ name์— ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ name์„ ์ „๋‹ฌํ•œ๋‹ค.

 

๐Ÿ“ Jinja2

๋ Œ๋”๋ง ํ…œํ”Œ๋ฆฟ์„ ํ†ตํ•ด ํ™”๋ฉด์„ ์ถœ๋ ฅํ•  ๋•Œ html์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ Jinja2 ๋ฌธ๋ฒ•์œผ๋กœ ํ•ด๋‹น ๋‚ด์šฉ์„ ๋„์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

{% for row in rows %}
<tr>
    <td>{{ loop.index }}</td>
    <td>{{ row[0] }}</td>
    <td>{{ row[1] }}</td>
</tr>
{% endfor %}


<!--
	์ผ๋ฐ˜์ ์ธ ํ˜•์‹
    
	{% if๋ฌธ or for๋ฌธ %}
    
    {{ ๋ณ€์ˆ˜๋‚˜ ํ‘œํ˜„์‹}}
    	...	....
    {% endif ๋˜๋Š” endfor %}
-->

Docs : jinja.palletsprojects.com/en/2.11.x/templates/

 

Template Designer Documentation — Jinja Documentation (2.11.x)

This document describes the syntax and semantics of the template engine and will be most useful as reference to those creating Jinja templates. As the template engine is very flexible, the configuration from the application can be slightly different from t

jinja.palletsprojects.com

 

๐Ÿ“ ๋ธ”๋ฃจ ํ”„๋ฆฐํŠธ

Flask์˜ ์š”์ฒญ์œผ๋กœ URL์„ ์ƒ์„ฑ ํ•  ๋•Œ ํ™”๋ฉด์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ธ”๋ฃจ ํ”„๋ฆฐํŠธ์™€ ์—ฐ๊ฒฐํ•œ๋‹ค.

๋ธ”๋ฃจ ํ”„๋ฆฐํŠธ๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐœ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์š”๊ตฌํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋Šฅ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋˜ํ•œ, ํŒŒ์ผ์„ ์—ฌ๋Ÿฌ๊ฐœ๋กœ ๋‚˜๋ˆ„์–ด ์œ ์ง€ ๋ณด์ˆ˜ ์ธก๋ฉด์—์„œ ๋งค์šฐ ํšจ๊ณผ์ ์œผ๋กœ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ธ”๋ฃจ ํ”„๋ฆฐํŠธ ์—†์ด flask ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์ด ํฌํ•จ๋œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ œ์ž‘ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๋ธ”๋ฃจํ”„๋ฆฐํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound

simple_page = Blueprint('simple_page', __name__,
                        template_folder='templates')

@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
    try:
        return render_template('pages/%s.html' % page)
    except TemplateNotFound:
        abort(404)

@simple_page.route ์žฅ์‹์ž๋ฅผ ์ด์šฉํ•ด ํ•จ์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋ฉด ๊ตฌํ˜„ํ•œ show() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ธ”๋ฃจ ํ”„๋ฆฐํŠธ๊ฐ€ ๋™์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค.
๋งŒ์•ฝ ๋ธ”๋ฃจ ํ”„๋ฆฐํŠธ ๋“ฑ๋กํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด register_blueprint() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

from flask import Flask
from yourapplication.simple_page import simple_page

app = Flask(__name__)
app.register_blueprint(simple_page)

 

๐Ÿ“ ์˜ˆ์ œ ) ๊ฐ„๋‹จํ•œ ๊ฒŒ์‹œํŒ ( CRUD )

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)
board = []

# ๊ฒŒ์‹œํŒ ๋‚ด์šฉ ์กฐํšŒ (Read)
@app.route('/')
def index():
    return render_template('list.html', rows=board)

# ๊ฒŒ์‹œํŒ ๋‚ด์šฉ ์ถ”๊ฐ€ (Create)
@app.route('/add',methods=["POST"])
def add():
    if request.method == "POST":
        name = request.form["name"]
        context = request.form["context"]
        board.append([name,context])
        return redirect(url_for("index"))
    else:
        return render_template("list.html", rows=board)

# ๊ฒŒ์‹œํŒ ๋‚ด์šฉ ๊ฐฑ์‹  (Update)
@app.route('/update/<int:uid>', methods=["GET","POST"])
def update(uid):
    if request.method == "POST":
        index = uid - 1
        name = request.form["name"]
        context = request.form["context"]
        
        board[index] = [name,context]   # ๊ธฐ์กด์˜ board๋‚ด์šฉ์— ๋ฎ์–ด์“ฐ๊ธฐ
        return redirect(url_for("index"))
    else:
        return render_template("update.html",index=uid,rows=board)

# ๊ฒŒ์‹œํŒ ๋‚ด์šฉ ์‚ญ์ œ (Delete)
@app.route('/delete/<int:uid>')
def delete(uid):
    index = uid - 1
    del board[index]

    return redirect(url_for("index"))


if __name__ == '__main__':
    app.run(debug=True)
<!-- ./template/list.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4>์ถ”๊ฐ€</h4>
    <form action="/add" method="POST">
        ์ด๋ฆ„<br>
        <input type="text" name="name" /><br>
        ๋‚ด์šฉ<br>
        <input type="text" name="context" style="text-align:center; width:400px; height:100px;" /><br><br>
        <input type="submit" value="๊ฒŒ ์‹œ" /><br>
    </form>
    <h4>๋ชฉ๋ก๋ณด๊ธฐ</h4>
    <table border=1 width="600" align="center">
        <thead>
            <td>๋ชฉ์ฐจ</td>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
            <td>์ˆ˜์ •, ์‚ญ์ œ</td>
        </thead>
        {% for row in rows %}
        <tr>
            <td>{{ loop.index }}</td>
            <td>{{ row[0] }}</td>
            <td>{{ row[1] }}</td>
            <td><a href="{{url_for('update', uid = loop.index)}}">์ˆ˜์ •</a> <a
                    href="{{url_for('delete', uid = loop.index)}}">์‚ญ์ œ</a></td>
        </tr>
        {% endfor %}
    </table>
</body>

</html>
<!-- ./templates/update.html -->

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="Generator" content="EditPlus®">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>SQLite3 ๊ฒŒ์‹œํŒ ๋“ฑ๋ก</title>

    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>๊ฒŒ์‹œํŒ</h3>
    <h4>์ˆ˜์ •</h4>
    <form action="/update/{{index}}" method="POST">
        ์ด๋ฆ„<br>
        <input type="text" name="name" /><br>
        ๋‚ด์šฉ<br>
        <input type="text" name="context" style="text-align:center; width:400px; height:100px;" /><br><br>
        <input type="submit" value="์ˆ˜ ์ •" /><br>
    </form>
    <h4>๊ธฐ์กด</h4>
    <table border=1 width="600" align="center">
        <thead>
            <td>๋ชฉ์ฐจ</td>
            <td>์ด๋ฆ„</td>
            <td>๋‚ด์šฉ</td>
        </thead>
        <tr>
            <td>{{ index}}</td>
            <td>{{ rows[index-1][0] }}</td>
            <td>{{ rows[index-1][1] }}</td>
        </tr>
    </table>
</body>

</html>

[ ์ถœ์ฒ˜ : elice ]

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts