■ JITHub 개발일지 25일차
□ TIL(Today I Learned) ::
django 클론코딩 - 인스타그램
1. 회원 탈퇴 기능
현재 회원정보를 가져와서 비밀번호 입력 후 회원탈퇴처리를 한다.
자연스러운 계정 삭제를 위해서 비밀번호 입력 후 재질문을 위해 모달창을 띄웠다.
<div>
<form action='{% url 'account_delete' %}' method="post">{% csrf_token %}
<div style="width: 700px; height:50px;float: left;padding-left:180px; margin-bottom:20px;">
<p>정말 회원 탈퇴하시겠습니까?<br>
탈퇴하시려면 비밀번호 입력 후 '회원탈퇴' 버튼을 클릭하세요!</p>
</div>
<div style="width: 150px; height:50px;float: left;">
<p style="font-size: 17px; text-align: right;">비밀번호</p>
</div>
<div style="width: 75%; float: left;padding-left:30px">
<div style="width:300px; height:50px;">
<input class="form-control" type='password' name='pw_del'>
{% for error in form.oldpassword.errors %}
<div class="error-message">{{ error }}</div>
{% endfor %}
</div>
</div>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" style="margin:0 0 20px 180px;background-color:rgb(0, 149, 247);" data-bs-toggle="modal" data-bs-target="#exampleModal">
회원 탈퇴
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">회원탈퇴</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
정말 탈퇴하시겠습니까?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
<button type="submit" class="btn btn-primary">확인</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
※ forms.py
회원탈퇴기능을 위해 password를 체킹하는 form을 아래와 같이 작성하였다.
# 회원 탈퇴를 위한 패스워드 Check Form
class CheckPasswordForm(forms.Form):
password = forms.CharField(label='비밀번호', widget=forms.PasswordInput(
attrs={'class': 'form-control',}),
)
def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = user
def clean(self):
cleaned_data = super().clean()
password = cleaned_data.get('password')
confirm_password = self.user.password
if password:
if not check_password(password, confirm_password):
self.add_error('password', '비밀번호가 일치하지 않습니다.')
기능의 기반이 되는 모델은 아래와 같다.
from django.db import models
from django.contrib.auth.models import AbstractUser
from .validators import validate_no_special_characters #유효성 검사 커스터마이징 import
# Create your models here.
#User model에서 username과 profile_image, intro, following 정의
class User(AbstractUser):
username = models.CharField(
max_length=15, #최대 15자
unique=True, #중복 허용 x
null=True, #null값 허용
validators=[validate_no_special_characters], #비밀번호 유효성 검사
error_messages={"unique":"이미 사용중인 닉네임입니다."}, #중복일 때 error메시지 뜨게함
)
profile_image = models.ImageField(
default="default_profile_pic.jpg",upload_to="profile_pics"
)
intro = models.CharField(max_length=60, blank=True)
following = models.ManyToManyField('self', symmetrical=False, blank=True, related_name= 'followers')
def __str__(self):
return self.email
※ 계정삭제를 위한 view 코드이다.
# 계정 삭제
def account_delete(request):
if request.method == "POST":
pw_del = request.POST['pw_del']
user = request.user
if check_password(pw_del, user.password):
user.delete()
return redirect('/')
return render(request, 'account/account_delete.html')
□ TIF(Today I Felt) ::
- 장고를 활용하여 인스타 클론코딩을 진행중이다.
- 좋아요부터 팔로우 기능, 회원 탈퇴기능까지 구현하고, DM기능까지 구현해보려했으나, 쉽지않았다.
- 제네릭 뷰, 장고 allauth, 그리고 채팅을 위한 Channels 패키지 등 장고를 편리하기 사용하기 위한 패키지들은 많이 나와있다. 하지만, 초기에 장고를 익힐 때에는 가장 기본적인 코드들로 짜보는 것이 더 도움이 되었을 수도 있겠다는 생각이 들었다. 또, 이러한 패키지들을 사용하게되면 생산성은 높지만, 확장성, 재사용성이 떨어지는 단점도 있다.
- 클린 코드를 작성해보려는 노력이 필요하다.
- 팀원들과 프로젝트를 진행하면서 짧은기간 부족한 상태에서 진행했지만, 많은 것을 배우고 생각하게 되는 프로젝트였던 것 같다.
반응형
'DEV > Web 개발' 카테고리의 다른 글
Web 개발 :: 파이썬 django 인스타그램 클론 코딩_TIL#26 (2) | 2022.10.06 |
---|---|
Web 개발 :: 파이썬 django 프로젝트(Project), 앱(App)의 구조 (0) | 2022.10.06 |
Web 개발 :: 파이썬 django 인스타그램 클론 코딩, aullauth _TIL#24 (0) | 2022.10.05 |
파이썬 웹 프로그래밍 :: 10월 첫째주 WIL #05 (0) | 2022.10.04 |
Web 개발 :: 파이썬 django 인스타그램 클론 코딩, aullauth _TIL#23 (1) | 2022.10.04 |
댓글