남기면 좋잖아
8. 장고 기본 인증 구현 본문
반응형
DRF-React Study
Chapter 6. 장고 기본 인증 구현하기
로그인 처리
장고 기본 제공기능 사용하기
from django.contrib.auth.views import LoginView from django.urls import path
urlpatterns = [
path('login/', LoginView.as_view(template_name='accounts/login_form.html'), name='login'),
]
```
사용자 프로필 페이지 및 프로필 수정
accounts/views.py
from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import render, redirect from .forms import ProfileForm from .models import Profile # Create your views here. # @login_required # def profile(request): # return render(request, 'accounts/profile.html') from django.views.generic import TemplateView, UpdateView
class ProfileView(LoginRequiredMixin, TemplateView):
template_name = 'accounts/profile.html'
profile = ProfileView.as_view()
@login_required
def profile_edit(request):
try:
profile = request.user.profile
except:
profile = None
if request.method == 'POST':
form = ProfileForm(request.POST, request.FILES, instance=profile)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
return redirect('profile')
else:
form = ProfileForm(instance=profile)
return render(request, 'accounts/profile_form.html', {
'form': form
})
```
반응형
'Programming > Django' 카테고리의 다른 글
10. 장고 DRF를 활용한 웹 API 만들기 (0) | 2020.07.28 |
---|---|
9. 비SPA 방식으로 인스타그램 St 만들기 (0) | 2020.07.19 |
7. 장고 Forms(2) (0) | 2020.07.17 |
7. 장고 Forms (0) | 2020.07.16 |
6. 웹 프론트엔드 기초 및 장고 static (0) | 2020.07.16 |
Comments