1. 程式人生 > >django-基於類登入

django-基於類登入

from django.views.generic.base import View


class LoginView(View):
    def get(self, request):
        return render(request, 'login.html', {})

    def post(self, request):
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        
if user is not None: login(request, user) return render(request, 'index.html', {'user': user}) else: return render(request, 'login.html', {'fail': '使用者名稱或密碼錯誤!'})

urls中需要修改 

path('login/', views.LoginView.as_view(), name='login'),