📢 공지사항
home

get_success_url 함수 그리고 리팩토링

수강계획
2022/01/13
번호
33
복습여부
수강여부
수강일
2022/01/17
속성
success url 설정할 때 detail로 변경했을 경우, pk 값이 가야하는데 동적으로 보내줄 수가 없음
따라서 내부 메서드 수정 필요!
‘pk’ : self.object.user.pk - object가 가르키는거 profile
edit 로그아웃 되어있을때 누구나 보이기때문에 수정 필요함
from django.shortcuts import render # Create your views here. from django.urls import reverse_lazy, reverse from django.utils.decorators import method_decorator from django.views.generic import CreateView, UpdateView from profileapp.decorators import profile_ownership_required from profileapp.forms import ProfileCreationForm from profileapp.models import Profile class ProfileCreateView(CreateView): model = Profile context_object_name = 'target_profile' form_class = ProfileCreationForm # success_url = reverse_lazy('accountapp:detail') template_name = 'profileapp/create.html' def form_valid(self, form): temp_profile = form.save(commit=False) temp_profile.user = self.request.user temp_profile.save() return super().form_valid(form) def get_success_url(self): return reverse('accountapp:detail', kwargs={'pk': self.object.user.pk}) @method_decorator(profile_ownership_required, 'get') @method_decorator(profile_ownership_required, 'post') class ProfileUpdateView(UpdateView): model = Profile context_object_name = 'target_profile' form_class = ProfileCreationForm template_name = 'profileapp/update.html' def get_success_url(self): return reverse('accountapp:detail', kwargs={'pk': self.object.user.pk})
Python
복사
views.py
{% 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: 'NanumSquareB'"> {{ 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: 'NanumSquareB'"> 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> <a href="{% url 'accountapp:delete' pk=user.pk %}"> <p> Quit </p> </a> {% endif %} </div> </div> {% endblock %}
HTML
복사
account/detail.html