UpdateView를 이용한 비밀번호 변경 구현
먼저 views.py에 UpdateView를 만들어준다.
class AccountUpdateView(UpdateView):
model = User
form_class = AccountUpdateForm
success_url = reverse_lazy('accountapp:hello_world')
template_name = 'accountapp/Update.html'
여기서 form_class에 UserCreationForm를 사용해도 되지만 UserCreationForm를 사용할 경우 아이디도 변경이 가능해져서 아이디 변경을 막기 위해 AccountUpdateForm을 새로 만들어준다.
class AccountUpdateForm(UserCreationForm):
def __init__(self,*arge,**kwargs):
super().__init__(*arge,**kwargs)
self.fields['username'].disabled = True
UserCreationForm를 상속하는 AccountUpdateForm을 만들어준다
self.fields['username'].disabled = True 를 통해서 아이디 변경을 막아준다.
Mypage 버튼을 누르면 비밀번호를 변경할 수 있는 Change Info를 만들어준다.
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div style="text-align: center; max-width: 500px; margin: 4rem auto">
<div class="mb-4">
<h4>Change Info</h4>
</div>
<form action="{% url 'accountapp:update' pk=user.pk %}" method="post">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-dark rounded-pill col-6 mt-3">
</form>
</div>
{% endblock %}
DeleteView 기반 회원 탈퇴 구현
마찬가지로 views.py에 DeleteView를 만들어 준다.
class AccountDeleteView(DeleteView):
model = User
success_url = reverse_lazy('accountapp:login')
template_name = 'accountapp/delete.html'
마찬가지로 delete.html을 만들어준다.
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div style="text-align: center; max-width: 500px; margin: 4rem auto">
<div class="mb-4">
<h4>Quit</h4>
</div>
<form action="{% url 'accountapp:delete' pk=user.pk %}" method="post">
{% csrf_token %}
<input type="submit" class="btn btn-danger rounded-pill col-6 mt-3">
</form>
</div>
{% endblock %}
'기타 > django' 카테고리의 다른 글
작정하고 장고 - decorator, superuser, media (0) | 2023.05.17 |
---|---|
작정하고 장고 - Authentication 인증시스템 구축 (0) | 2023.05.15 |
작정하고 장고 - DetailView를 활용한 개인 페이지 구현 (0) | 2023.05.11 |
작정하고 장고 - Bootstrap을 이용한 Form 디자인 정리 (0) | 2023.05.10 |
작정하고 장고 - login, logout 구현 (0) | 2023.05.09 |
댓글