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'),