728x90
반응형
몇가지의 수정사항
1
class ProfileCreateView(CreateView):
model = Profile
context_object_name = 'target_profile'
form_class = ProfileCreationForm
success_url = reverse_lazy('accountapp:hello_world')
template_name = 'profileapp/create.html'
보면 success_url로 hello_world를 넘겨주고 있는데 detail로 넘어가는게 더 자연스러워보인다.
하지만 accountapp:detail.html로 코드를 짜면 작동하지 않게되는데 그 이유는
urlpatterns = [
path('hello_world/', hello_world, name='hello_world'),
path('login/', LoginView.as_view(template_name='accountapp/login.html'), name='login'),
path('logout/', LogoutView.as_view(), name='logout'),
path('create/', AccountCreateView.as_view(), name='create'),
path('detail/<int:pk>', AccountDetailView.as_view(), name='detail'),
path('update/<int:pk>', AccountUpdateView.as_view(), name='update'),
path('delete/<int:pk>', AccountDeleteView.as_view(), name='delete'),
]
detail은 pk를 추가적으로 제공해주어야하기 때문.
내부 메서드를 수정해주자. (get_success_url(self))
def get_success_url(self):
return reverse('accountapp:detail', kwargs={'pk': self.object.user.pk})
views.py
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
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})
728x90
반응형
'웹 프로그래밍' 카테고리의 다른 글
[pinterest clone (27)] articleapp 구현 (0) | 2021.05.01 |
---|---|
[pinterest clone (26)] Articleapp 화면구성 (MagicGrid) (0) | 2021.05.01 |
[pinterest clone (24)] profileapp update view (0) | 2021.04.30 |
[pinterest clone (23)] profileapp create view (0) | 2021.04.30 |
[pinterest clone (22)] ModelForm (0) | 2021.04.30 |