Tags
- Article & User
- Queue
- Django
- 그리디
- 완전검색
- outer join
- Tree
- ORM
- regexp
- N:1
- 쟝고
- 이진트리
- count
- migrations
- create
- delete
- update
- 통계학
- 스택
- Vue
- stack
- 큐
- distinct
- drf
- 백트래킹
- DB
- M:N
- 뷰
- 트리
- SQL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Notice
Recent Posts
Link
데이터 분석 기술 블로그
Django에 대하여(14)_ORM with view(Read) 본문
1. Read
1-1 전체 게시글 조회
# articles/views.py
from .models import Article
def index(request):
articles = Article.objects.all()
context = {
'articles': articles,
}
return render(request, 'articles/index.html', context)
<!-- articles/index.html -->
<h1>Articles</h1>
<hr>
{% for article in articles %}
<p>글 번호: {{ article.pk }}</p>
<p>글 제목: {{article.title }}</p>
<p>글 내용: {{article.content }}</p>
<hr>
{% endfor %}
1-2. 단일 게시글 조회
# articles/urls.py
urlpatterns = [
...
path('<int:pk>/', views.detail, name='detail'),
]
# articles/views.py
def detail(request, pk):
article = Article.objects.get(pk=pk)
context = {
'articles': articles,
}
return render(request, 'articles/detail.html', context)
<!-- templates/articles/detail.html -->
<h2>DETAIL</h2>
<h3>{{ articles.pk }} 번째 글</h3>
<hr>
<p>제목: {{article.title }}</p>
<p>내용: {{article.content }}</p>
<p>작성일: {{ article.created_at }}</p>
<p>수정일: {{ article.updated_at }}</p>
<hr>
<a href="{% url 'articles:index' %}">[back]</a>
1-3. 단일 게시글 페이지 링크 작성
<!-- templates/articles/detail.html -->
<h1>Articles</h1>
<hr>
{% for article in articles %}
<p>글 번호: {{ article.pk }}</p>
<a href="{% url 'articles:detail' article.pk %}
<p>글 제목: {{article.title }}</p>
</a>
<p>글 내용: {{article.content }}</p>
<hr>
{% endfor %}
'백엔드' 카테고리의 다른 글
Django에 대하여(16)_ORM with view(Delete와 Update) (0) | 2024.04.06 |
---|---|
Django에 대하여(15)_ORM with view(Create) (0) | 2024.04.05 |
Django에 대하여(13)_QuerySet API 실습 (0) | 2024.04.03 |
Django에 대하여(12)_ORM과 QuerySet API (0) | 2024.04.02 |
Django에 대하여(11)_모델 필드와 Admin site (0) | 2024.04.01 |