작정하고 장고 26강 - DeleteView 기반 회원탈퇴 구현

IkSun·2023년 5월 4일

작정하고 장고

목록 보기
26/46

DeleteView 작성

# pargmatic/accountapp/views.py

from django.views.generic import CreateView, DetailView, UpdateView, DeleteView

class AccountDeleteView(DeleteView):
    model = User
    context_object_name = 'target_user'
    success_url = reverse_lazy('accountapp:login')
    template_name = 'accountapp/delete.html'
  • 라우팅
# pragmatic/accountapp/urls.py
from accountapp.views import hello_world, AccountCreateView, AccountDetailView, AccountUpdateView, AccountDeleteView

urlpatterns = [
	...
    path('delete/<int:pk>', AccountDeleteView.as_view(), name='delete'),
]

delete.html 만들기

  • 안에 들어가는 form 은 없다.
<!--pragmatic/accountapp/templates/accountapp/delete.html-->

{% extends 'base.html' %}

{% block content %}

  <div style="text-align : center; max-width: 500px; margin: 4rem auto;">>
    <div class="mb-4"> <!-- margin bottom 해서 4배 -->
      <h4>Quit</h4>
    </div>
    <!--url 일원화, pk=user.pk->pk=target_user.pk 으로 수정 post 방식 으로 전송 -->
    <form action="{% url 'accountapp:delete' pk=target_user.pk %}" method="post">  
      {% csrf_token %}  <!-- csrf_token 은 항상 들어 가야 하는 것 -->
      <input type="submit" class="btn btn-dark rounded-pill col-6 mt-3">
    </form>
  </div>

{% endblock %}

Detail.html 수정

  • 본인이라면 링크를 생성하도록
  • 따라서 Mypage 로 들어가면은 quit 링크가 생성됨을 볼 수 있다.
  • 제출 색 변경 : <input type="submit" class="btn btn-danger rounded-pill col-6 mt-3"> \to 자세한 건 부트스트랩 홈페이지 참고
  • 제출을 누르면 계정이 없어진다 \to 탈퇴가 완료됨.
{% extends 'base.html'%}

{% block content %}

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

      {% if target_user == user %}
      <a href="{% url 'accountapp:update' pk=user.pk%}">
        <p>
          Change Info
        </p>
      </a>
      <!--추가한 부분 : 탈퇴로 향하는 링크 생성-->
      <a href="{% url 'accountapp:delete' pk=user.pk%}">
        <p>
          Quit
        </p>
      </a>
      {% endif %}
    </div>
  </div>

{% endblock %}

  • 회원 가입을 하는 SignUp 링크를 추가하기
    <div class="pragmatic_header">
       <div>
            <h1 class="pragmatic_logo">Prgmatic</h1>
       </div>
        <div>
            <span>nav1</span>
            <span>nav2</span>
            <span>nav3</span>
            {% if not user.is_authenticated %}
            <a href="{% url 'accountapp:login' %}?next={{ request.path }}">
                <span>login</span>
            </a>
            <!--추가한 부분-->
            <a href="{% url 'accountapp:create' %}">
                <span>SignUp</span>
            </a>
            {% else %}
            <a href="{% url 'accountapp:detail' pk=user.pk %}" >
                <span>MyPage</span>
            </a>
            <a href="{% url 'accountapp:logout' %}?next={{ request.path }}">
                <span>logout</span>
            </a>
            {% endif %}
        </div>
    </div>

commit

profile
공부한 것 기록용

0개의 댓글