DB
DB에 대하여(18)_프로필 기능 구현 (feat. Django)
데이터분석가 이채은
2024. 5. 5. 20:53
프로필 페이지
각 회원의 개인 프로필 페이지에 팔로우 기능을 구현하기 위해 프로필 페이지를 먼저 구현해 봅시다.
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>
프로필 페이지 결과를 확인합니다.