☘️ UpdateView를 이용한 비밀번호 변경 구현
UpdateView
를 사용하여 프로필 정보를 업데이트하는 AccountUpdateView
를 정의합니다.
model
속성에는 User
모델을 지정합니다. form_class
속성에는 프로필 정보를 입력받기 위해 생성한 AccountUpdateForm
을 지정합니다.
success_url
속성은 프로필 정보 업데이트가 성공한 경우 리디렉션할 URL을 지정합니다.
template_name
속성은 업데이트 페이지의 템플릿 파일 경로를 지정합니다.
- URL 패턴은
/update/<int:pk>/
로 설정되어 해당 프로필을 업데이트할 사용자의 고유한 기본 키를 받아옵니다.
update.html
템플릿 파일을 작성합니다. 사용자 입력을 받아 프로필 정보를 업데이트하는 폼을 구성합니다. 폼의 action
속성에 pk=user.pk
를 포함하여 URL에 사용자의 고유한 기본 키를 전달합니다.
detail.html
템플릿 파일에 "Update Profile" 버튼을 추가하여 사용자가 자신의 프로필 정보를 변경할 수 있는 링크를 제공합니다. 이때, 현재 로그인한 사용자와 대상 프로필이 동일한 경우에만 버튼이 표시되도록 target_user == user
조건을 사용합니다.
AccountUpdateForm
은 UserCreationForm
을 상속받아 프로필 정보 업데이트 폼을 정의합니다. __init__
메서드를 오버라이드하여 username
필드를 비활성화하고 입력할 수 없도록 설정합니다.
© View
from accountapp.forms import AccountUpdateForm
class AccountUpdateView(UpdateView):
model = User
form_class = AccountUpdateForm
success_url = reverse_lazy('accountapp:hello_world')
template_name = 'accountapp/update.html'
© Template
{% extends 'base.html' %}
{% block content %}
<div>
<div style="text-align: center; max-width: 500px; margin: 4rem auto;">
<p>
{{target_user.date_joined}}
</p>
<h2 style="font-family: 'NanumSquareB'">
{{target_user.username}}
</h2>
{% if target_user == user %}
<a href="{% url 'accountapp:update' pk=user.pk %}">
<p>Update Profile</p>
</a>
{% endif %}
</div>
</div>
{% endblock %}
from django.contrib.auth.forms import UserCreationForm
class AccountUpdateForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['username'].disabled = True
☘️ DeleteView 기반 회원탈퇴 구현
DeleteView
를 사용하여 계정을 삭제하는 AccountDeleteView
를 정의합니다.
model
속성에는 User
모델을 지정합니다. success_url
속성에는 계정 삭제가 성공한 경우 리디렉션할 URL을 지정합니다.
template_name
속성은 삭제 페이지의 템플릿 파일 경로를 지정합니다.
- URL 패턴은
/delete/<int:pk>/
로 설정되어 삭제할 계정의 고유한 기본 키를 받아옵니다.
delete.html
템플릿 파일을 작성합니다. 계정을 삭제하기 위한 폼을 구성합니다. 폼의 action
속성에 pk=user.pk
를 포함하여 URL에 사용자의 고유한 기본 키를 전달합니다.
detail.html
템플릿 파일에 "Delete" 버튼을 추가하여 사용자가 자신의 계정을 삭제할 수 있는 링크를 제공합니다. 이때, 현재 로그인한 사용자와 대상 계정이 동일한 경우에만 버튼이 표시되도록 target_user == user
조건을 사용합니다.
© View
class AccountDeleteView(DeleteView):
model = User
success_url = reverse_lazy('accountapp:login')
template_name = 'accountapp/delete.html'
© Detail
{% extends 'base.html' %}
{% block content %}
<div>
<div style="text-align: center; max-width: 500px; margin: 4rem auto;">
<p>
{{target_user.date_joined}}
</p>
<h2 style="font-family: 'NanumSquareB'">
{{target_user.username}}
</h2>
{% if target_user == user %}
<a href="{% url 'accountapp:update' pk=user.pk %}">
<p>Update Profile</p>
</a>
<a href="{% url 'accountapp:delete' pk=user.pk %}">
<p>Delete</p>
</a>
{% endif %}
</div>
</div>
{% endblock %}