Backend/Django

django 21. get_success_url 함수 그리고 리팩토링

meong_j 2021. 10. 6. 19:13
728x90
반응형

get_success_url

success_url = reverse_lazy('accountapp:detail')
  • 기존의 success_url

 

    def get_success_url(self):
        return reverse('accountapp:detail', kwargs={'pk': self.object.user.pk})
  • 변경 후 get_success_url 함수
  • 자신의 페이지로 이동하기 위함
  • self.object 는 profile 지칭
  • profile의 user의 pk를 찾아서 넘겨줌
  • user 본인의 detail 페이지로 이동

 

detail.html

{% extends 'base.html' %}
{% load bootstrap4 %}

{% block content %}

<div>
    <div style="text-align: center; max-width: 500px; margin: 4rem auto;">

        {% if target_user.profile %}
        <img src="{{ target_user.profile.image.url }}" alt="" style="height: 12rem; width: 12rem; border-radius: 20rem; margin-bottom: 2rem;">

         <h2 style="font-family: NanumBarunpenB">
            {{ target_user.profile.nickname }}
             {% if target_user == user %}
             <a href=" {% url 'profileapp:update' pk=target_user.profile.pk %}">
                 edit
             </a>
             {% endif %}
        </h2>

         <h5 style="margin-bottom: 3rem">
            {{ target_user.profile.message }}
        </h5>

        {% else %}
            {% if target_user == user %}
        <a href="{% url 'profileapp:create' %}">
            <h2 style="font-family: NanumBarunpenB">
                Create Profile
            </h2>
        </a>
            {% else %}
            <h2>
                닉네임 미설정
            </h2>
            {% endif %}
        {% endif %}


        {% if target_user == user %}
        <a href="{% url 'accountapp:update' pk=user.pk %}">
            <p>
                Change Info
            </p>
        </a>
        {% endif %}

        {% if target_user == user %}
        <a href="{% url 'accountapp:delete' pk=user.pk %}">
            <p>
                Quit
            </p>
        </a>
        {% endif %}

    </div>

</div>

{% endblock %}

 

 

  • 로그아웃 했을 경우 혹은 다른 user의 profile 페이지로 이동했을 때 화면
  • edit, 개인정보 수정, quit 기능 비활성화

 

  • 닉네임 설정하지 않은 user로 접근시 보이는 화면페이지
반응형