django-基於類登入
阿新 • • 發佈:2018-11-28
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'),