Tags
- Tree
- 백트래킹
- 뷰
- 그리디
- 완전검색
- 큐
- drf
- 쟝고
- distinct
- Vue
- create
- migrations
- Django
- regexp
- update
- N:1
- Queue
- M:N
- 이진트리
- 트리
- 통계학
- outer join
- Article & User
- count
- ORM
- delete
- stack
- 스택
- SQL
- DB
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
데이터 분석 기술 블로그
DB에 대하여(18)_프로필 기능 구현 (feat. Django) 본문
프로필 페이지
각 회원의 개인 프로필 페이지에 팔로우 기능을 구현하기 위해 프로필 페이지를 먼저 구현해 봅시다.
url을 작성합니다.
# accounts/urls.py
urlpatterns = [
...
path('profile/<username>/', views.profile, name='profile'),
]
view 함수를 작성합니다.
# accounts/views.py
from django.contrib.auth import get_user_model
def profile(request, username):
User = get_user_model()
person = User.objects.get(username=username)
context = {
'person': person,
}
return render(request, 'accounts/profile.html', context)
profile 템플릿을 작성합니다.
<!-- accounts/profile.html -->
<h1>{{ person.username }}님의 프로필</h1>
<hr>
<h2>{{ person.username }} 가 작성한 게시글</h2>
{% for arrticle in person.article_set.all %}
<div>{{ article.title }}</div>
{% endfor %}
<hr>
...
<h2>{{ person.username }} 가 작성한 댓글</h2>
{% for comment in person.comment_set.all %}
<div>{{ comment.content }}</div>
{% endfor %}
<hr>
<h2>{{ person.username }} 가 좋아요한 게시글</h2>
{% for article in person.like_articles.all %}
<div>{{ article.title }}</div>
{% endfor %}
프로필 페이지로 이동할 수 있는 링크를 작성합니다.
<!-- articles/index.html -->
<a href="{% url 'accounts:profile' user.username %}">내 프로필</a>
<p>작성자 : <a href="{% url 'accounts:profile' article.user.username %}">{{ article.user }}</a></p>
프로필 페이지 결과를 확인합니다.
'DB' 카테고리의 다른 글
DB에 대하여(20)_Fixtures (feat. Django) (0) | 2024.05.07 |
---|---|
DB에 대하여(19)_팔로우 기능 구현 (feat. Django) (2) | 2024.05.06 |
DB에 대하여(17)_좋아요 기능 구현 (feat. Django) (0) | 2024.05.04 |
DB에 대하여(16)_좋아요 모델 관계 설정 (feat. Django) (0) | 2024.05.03 |
DB에 대하여(15)_Django ManyToManyField (feat. Django) (0) | 2024.05.02 |