파이썬 앱파일에 테스트용 더미 데이터를 만들어보자
딕셔너리 형식의 리스트 파일
posts = [
{
'author' : '업철이',
'title' : '첫번째 블로그 포스트',
'content' : '첫번째 블로그 포스트 내용',
'date_posted' : 'Jan 16, 2023'
},
{
'author' : '제리',
'title' : '두번째 블로그 포스트',
'content' : '두번째 블로그 포스트 내용',
'date_posted' : 'Jan 18, 2023'
}
]
home.html에 posts 내용 불러들이기
posts는 render_template function
def hello():
return render_template('home.html', posts = posts)
home.html의 body 부분에 jinja2 템플릿 문법에 따라 작성
*** for loop 사용시 for loop 끝나는 부분을 명시해야 함 ***
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{% for post in posts %}
<h1>{{ post.title }}</h1>
<p>By {{ post.author}} on {{ post.date_posted }}</p>
<p>{{ post.content }}</p>
{% endfor %}
</body>
</html>
웹페이지 화면

페이지 소스 확인

'Flask' 카테고리의 다른 글
[Flask] 템플릿 상속 (0) | 2023.01.19 |
---|---|
[Flask] Jinja2 이용하여 if 조건문 사용하기 (0) | 2023.01.19 |
[Flask] Template 폴더 만들고 연결하기 (0) | 2023.01.18 |
[Flask] Flask install, debugger activate (0) | 2023.01.15 |
[Flask] 가상 환경 설정하고 실행하기 (0) | 2023.01.11 |