Python Flask Tutorial #2 - Templates
https://youtu.be/QnDWIZuWYW0?list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH
Flask에서 Templates로 웹 페이지 구성에 대한 설명입니다.
mkdir templates
그안에 home.html 만들고..
vscode에서 html:5 로 하면 HTML 골격이 나오죠..
flaskblog.py에서 render_template를 import하고
return render_template('home.html') 을 하면 템플리트 웹 페이지가 표시됩니다.
about 페이지도 같은 방식으로 만드세요.. about.html
다음으로는 template에 데이타를 넘겨주는 방법입니다.
posts = [
{
'authos': 'Corey Schafer',
'title': 'Blog Post 1',
'content': 'First post content',
'date_posted': 'April 20, 2018'
},
{
'authos': 'Jane Doe',
'title': 'Blog Post 2',
'content': 'Second post content',
'date_posted': 'April 21, 2018'
}
]
https://jinja.palletsprojects.com/en/2.10.x/templates/
control 문은 {% %} 으로 쓰고 변수는 {{ }} 이렇게 사용합니다.
render_template에 Post에 대한 것을 아래와 같이 넘겨주고
return render_template('home.html', posts=posts)
home.html에서
<body>
{% for post in posts %}
<h1>{{ post.title }}</h1>
<p>By {{ post.author}} on {{ post.date_posted}}</p>
<p>{{ post.content }}</p>
{% endfor %}
</body>
와 같이 받아 주면 됩니다.
home.html 과 about.html의 html 코드가 겹치는 부분은 layout.html으로 보내고
다른 부분만 아래처럼 처리함.
{% extends "layout.html" %}
{% block content %}
{% endblock content %}
다음으로 꾸미기인데 bootstrap을 최신것을 Copy & Paste로...
snippet 디렉토리에 미리 사용할 코드블럭을 만들어 두었다가 Copy & Paste로 하네요.
https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Flask_Blog/snippets
CSS 파일은
static 디렉토리 아래에 놓고, url_for를 사용해서 읽어옴.
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">

게시판 목록
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 91 | 4년 전 | 1161 | ||
| 90 | 5년 전 | 1293 | ||
| 89 | 5년 전 | 1856 | ||
| 88 | 5년 전 | 1894 | ||
| 87 | 5년 전 | 28249 | ||
| 86 | 5년 전 | 1423 | ||
| 85 | 5년 전 | 1288 | ||
| 84 | 5년 전 | 1306 | ||
| 83 | 5년 전 | 959 | ||
| 82 | 5년 전 | 1340 | ||
| 81 | 5년 전 | 1750 | ||
| 80 | 5년 전 | 1738 | ||
| 79 | 5년 전 | 1668 | ||
| 78 | 5년 전 | 1576 | ||
| 77 | 5년 전 | 1275 | ||
| 76 | 5년 전 | 1779 | ||
| 75 | 5년 전 | 1796 | ||
| 74 | 5년 전 | 3252 | ||
| 73 | 5년 전 | 3416 | ||
| 72 | 5년 전 | 2506 | ||
| 71 | 5년 전 | 1846 | ||
| 70 | 5년 전 | 2708 | ||
| 69 | 5년 전 | 1453 | ||
| 68 | 5년 전 | 1383 | ||
| 67 | 5년 전 | 1240 | ||
| 66 | 5년 전 | 1603 | ||
| 65 | 5년 전 | 2067 | ||
| 64 | 5년 전 | 1808 | ||
| 63 | 5년 전 | 1316 | ||
| 62 | 5년 전 | 1158 |

댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기