📢 데이터베이스에서 모든 아티클을 조회해서 /articles/에서 볼 수 있도록 해봅시다!
- view에서 model에 접근해 모든 아티클 가져오기
- view에서 가져온 아티클을 template으로 넘기기
from .models import Article
...
def articles(request):
articles = Article.objects.all()
context = {
"articles": articles,
}
return render(request, "articles.html", context)
...
3. template에서 넘어온 context 보여주기
{% extends "base.html" %}
{% block content %}
<h1>Articles</h1>
<ul>
{% for article in articles %}
<li>
<div>글 번호 : {{ article.id }}</div>
<div>글 제목 : {{ article.title }}</div>
<div>글 내용 : {{ article.content }}</div>
<br>
</li>
{% endfor %}
</ul>
{% endblock content %}
4. view에서 템플릿을 랜더링해서 리턴
'개발일기 [Python 파이썬]' 카테고리의 다른 글
[SQL 데이터에서 예상하지 못한 값이 나왔을 때 (이상한 값, 값이 없음 등) 5/5] (0) | 2024.04.23 |
---|---|
[SQL] Subquery 문 안을 수정해서, 음식 주문시간이 25분보다 초과한 시간을 가져오기 4/5 (0) | 2024.04.22 |
완전탐색기 코딩 (데이터 다 체크해서 원하는 최대값 찾기) (0) | 2024.04.17 |
장고 에러 발생시 해결 방안 (0) | 2024.04.12 |
[Python Algorithm] Installation of Base Station (0) | 2024.04.09 |