views.py의 버그를 수정해보자
class AccountDetailView(DetailView):
model = User
context_object_name = 'target_user'
template_name = 'accountapp/detail.html'
class AccountUpdateView(UpdateView):
model = User
context_object_name = 'target_user'
form_class = AccountUpdateForm
success_url = reverse_lazy('accountapp:hello_world')
template_name = 'accountapp/update.html'
class AccountDeleteView(DeleteView):
model = User
context_object_name = 'target_user'
success_url = reverse_lazy('accountapp:login')
template_name = 'accountapp/delete.html'
-cotext_object_name을 'target_user'로 입력하도록 한다
template
delete.html update.html 안에서 user.pk를 target_user.pk로 수정한다
urls.py
path('account/' ....)에서 account를 복수형인 accounts로 변경한다.
hello_world 페이지를 로그인시에만 접속할 수 있도록 수정
로그인이 안되었다면 로그인 페이지로 넘어가도록 코드를 작성해보자
def hello_world(request):
if request.user.is_authenticated:
if request.method == "POST":
text = request.POST.get('hello_world_input')
new_hello_world = HelloWorld()
new_hello_world.text = text
new_hello_world.save()
hello_world_list = HelloWorld.objects.all()
return HttpResponseRedirect(reverse('accountapp:hello_world'))
else:
hello_world_list = HelloWorld.objects.all()
return render(request, 'accountapp/hello_world.html', context={'hello_world_list': hello_world_list})
else:
return HttpResponseRedirect(reverse('accountapp:login'))
- if request.user.is_authenticated
사용자 인증이 되었다면 아래 코드를 실행하고
else 인증이 안되었다면 login 창으로 이동한다
문제점1. 로그인 하지 않아도 개인 프로필 수정 화면으로 진입 가능하다
문제점2. 그것이 남의 프로필이라도 진입 가능하다
문제를 해결하도록 하자
class AccountUpdateView(UpdateView):
model = User
context_object_name = 'target_user'
form_class = AccountUpdateForm
success_url = reverse_lazy('accountapp:hello_world')
template_name = 'accountapp/update.html'
def get(self, *args, **kwargs):
if self.request.user.is_authenticated and self.get_object() == self.request.user:
return super().get(*args, **kwargs)
else:
return HttpResponseForbidden()
def post(self, *args, **kwargs):
if self.request.user.is_authenticated and self.get_object() == self.request.user:
return super().post(*args, **kwargs)
else:
return HttpResponseForbidden()
-문제점 1 해결
if self.request.user.is_authenticated
-문제점 2 해결
and self.get_object() == self.request.user
-HttpResponseForbidden() : 차단
로그인이 안되었거나 혹은 본인이 아닌 다른 사람의 페이지에 접근할시 forbidden
profileapp 시작 그리고 ModelForm (0) | 2021.09.22 |
---|---|
Decorater를 이용한 코드 간소화 (0) | 2021.09.22 |
DeleteView기반 회원탈퇴 구현 (0) | 2021.08.17 |
UpdateView를 이용한 비밀번호 변경 구현 (0) | 2021.08.17 |
DetailView 마이페이지 구현 (0) | 2021.08.13 |
댓글 영역