๐ ๋ ๋๋ง ํ ํ๋ฆฟ
๋ ๋๋ง(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>
'python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Flask] (5) RDB - Flask Connection & ๊ฒ์ํ ์์ (0) | 2021.01.25 |
---|---|
[Flask] (4) ์ธ์ฆ & ๋ก๊ทธ์ธ๊ตฌํ & ๋ก๊น (0) | 2021.01.25 |
[Flask] (2) REST & HTTP Method & API/End Point (0) | 2021.01.22 |
[Flask] (1) Intro & URL Routing & Variable Rules & jsonify( ) (1) | 2021.01.21 |
[python] ๋ฌธ์์ด ์ ๊ทํํ์ ์ ๋ฆฌ (0) | 2021.01.14 |