get_success_url 함수 그리고 리팩토링 (작정하고 장고 33강)

_upper·2023년 5월 24일

33강

지금까지 짠 코드들의 문제를 조금 해결하고, get_success_url을 통해 동적인 redirect url을 반환해 주도록 변경해본다.

    def get_success_url(self):
        return reverse('accountapp:detail', kwargs={'pk': self.object.user.pk})

연결되어있는 profile 유저의 pk를 찾아서 넘겨준다

{% extends 'base.html' %}

{%  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: 'NanumSuqreB'">
                {{ 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: 'NanumSuqreB'">
                    {Create Profile
                    </h2>
                </a>
                {% else %}
                    닉네임 미설정
                {% endif %}
            {% endif %}
            {% 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 %}

detail.html 수정
로그인 되어 있지 않은데 edit 버튼 있는 걸 지우고
닉네임 미설정으로 표시하기

0개의 댓글