본문 바로가기
기타/django

작정하고 장고 - DetailView를 활용한 개인 페이지 구현

by 방배킹 2023. 5. 11.

DetailView를 활용한 개인 페이지 구현

class AccountDetailView(DetailView):
    model = User
    context_object_name = 'target_user'
    template_name = 'accountapp/detail.html'

Detail View를 만들어준다.(read)

 

 

path('detail/<int:pk>', AccountDetailView.as_view(), name='detail')

accountapp의 urls.py에 해당 path를 추가해준다 여기서 pk는 기본키를 의미한다.

 

 

{% extends 'base.html' %}

{% block content %}

<div style="text-align: center; max-width: 500px; margin: 4rem auto">
  <div>
    <p>
      {{ target_user.date_joined }}
    </p>
    <h2 style="font-family:'NanumGothic'">
      {{ target_user.username }}
    </h2>
  </div>
</div>

{% endblock %}

detail.html을 만들어준다.

 

 

<a href="{% url 'accountapp:detail' pk=user.pk %}">
            <span>Mypage</span>
        </a>

header.html에 Mypage를 볼 수 있도록 추가해준다.

 

 


로그인후 상단의 Mypage를 눌러주면

 

 

다음과 같이 가입 날짜와 아이디가 출력된다.

댓글