2019独角兽企业重金招聘Python工程师标准>>>
URLS:
urlpatterns = [url(r"^$", LoginView.as_view(), name="login"), ]
Views:class LoginView(LoginHandler):template_name = "login.html"
Handlers:from django.contrib.auth import authenticate, login, logout
Logout:
class LoginHandler(TemplateView):@get_requestdef post(self, request):username = self.data['username']password = self.data['password']user = authenticate(username=username, password=password)if user is not None:login(request, user)return redirect('/index/')else:return HttpResponse("Login Error")
HTML:
URLS:urlpatterns += [url(r"^logout/$", LogoutView.as_view(), name="logout"), ]
VIEWS:
class LogoutView(LogoutHandler):pass
Handlers:class LogoutHandler(TemplateView):def get(self, request):logout(request=request)return HttpResponseRedirect(redirect_to='/')