Django User obejct에 대해 이해하고 User CRUD 기능(회원 가입, 회원 탈퇴, 회원정보 수정, 비밀번호 변경)을 구현해보자!
class Meta: model = User
가 등록된 formclass UserCreationForm(forms.ModelForm):
...
class Meta:
model = User
fields = ("username",)
field_classes = {"username":, UsernameField}
# accounts/forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = get_user_model()
class CustomUserChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = get_user_model()
get_user_model()
을 사용해 참조해야 한다고 강조하고 있음# accounts/urls.py
from django.urls import path
from . import views
app_name = 'accounts'
urlpatterns = [
...,
path('signup/', views.signup, name='signup'),
]
# accounts/views.py
from .forms import CustomUserCreationForm
def signup(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
user = form.save()
return redirect('articles:index')
else:
form = CustomUserCreationForm()
context = {
'form':form
}
return render(request, 'accounts/signup.html', context)
<!-- base.html -->
<div id="nav">
<a href="{% url 'accounts:signup' %}">회원가입</a>
<a href="{% url 'accounts:login' %}">로그인</a>
<a href="{% url 'accounts:logout' %}">로그아웃</a>
</div>
<h3 id="user-hello"><i>안녕하세요, {{user}} 님 !</i></h3>
<hr>
<div id="content">
{% block content %}{% endblock content %}
</div>
# accounts/views.py
def signup(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
user = form.save()
auth_login(request, user) # 회원 가입 하면 자동 로그인
return redirect('articles:index')
else:
form = CustomUserCreationForm()
context = {
'form':form
}
return render(request, 'accounts/signup.html', context)
# accounts/urls.py
from django.urls import path
from . import views
app_name = 'accounts'
urlpatterns = [
...,
path('delete/', views.delete, name='delete'),
]
# accounts/views.py
# url(/accounts/delete/)로 접근해도 삭제 가능
def delete(request):
user = request.user
user.delete()
return redirect('articles:index')
# 코드 수정
def delete(request):
if request.method == 'POST':
user = request.user
user.delete()
auth_logout(request)
return redirect('articles:index')
else:
return redirect('articles:index')
<!-- base.html -->
...
<form action="{% url 'accounts:delete' %}" method="POST">
{% csrf_token %}
<input type="submit" value="회원탈퇴">
</form>
...
# accounts/views.py
def delete(request):
user = request.user
user.delete()
auth_logout(request)
return redirect('articles:index')
# accounts/forms.py
class CustomUserChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = get_user_model()
fields = ('email', 'first_name', 'last_name',)
# accounts/urls.py
from django.urls import path
from . import views
app_name = 'accounts'
urlpatterns = [
...,
path('update/', views.update, name='update'),
]
# accounts/views.py
from .forms import CustomUserChangeForm
def update(request):
if request.method == 'POST':
form = CustomUserChangeForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect('articles:index')
else:
form = CustomUserChangeForm(instance=request.user)
context = {
'form':form
}
return render(request, 'accounts/update.html', context)
<!-- accounts/update.html -->
{% extends 'base.html' %}
{% block content %}
<h1>회원정보수정</h1>
<form action="{% url 'accounts:update' %}" method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit">
</form>
{% endblock content %}
<!-- base.html -->
<div id="nav">
<a href="{% url 'accounts:signup' %}">회원가입</a>
<a href="{% url 'accounts:login' %}">로그인</a>
<a href="{% url 'accounts:logout' %}">로그아웃</a>
<a href="{% url 'accounts:update' %}">회원정보 수정</a>
<form action="{% url 'accounts:delete' %}" method="POST">
{% csrf_token %}
<input type="submit" value="회원탈퇴">
</form>
</div>
# accounts/urls.py
from django.urls import path
from . import views
app_name = 'accounts'
urlpatterns = [
...,
path('password/', views.change_password, name='change_password'),
]
# accounts/views.py
from django.contrib.auth.forms import PasswordChangeForm
def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
form.save()
return redirect('articles.index')
else:
form = PasswordChangeForm(request.user)
context = {
'form':form,
}
return render(request, 'account/change_password.html', context)
<!-- accounts/change_password.html -->
{% extends 'base.html' %}
{% block content %}
<h1>비밀번호 변경</h1>
<form action="{% url 'accounts:change_password' %}" method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="변경하기">
</form>
{% endblock content %}
# accounts/views.py
from django.contrib.auth import update_session_auth_hash
def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
form.save()
# 암호가 변경되어도 로그아웃 되지 않음
update_session_auth_hash(request, form.user)
return redirect('articles.index')
else:
form = PasswordChangeForm(request.user)
context = {
'form':form,
}
return render(request, 'accounts/change_password.html', context)