我正在Django 1.8中学习基于类的视图,并想知道是否有人可以在这里帮助我.我已经创建了一个基于函数的登录和注销视图,如下所示:
登录
def Login(request): if request.method == "POST": username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) return HttpResponseRedirect('/form') else: return HttpResponse("Inactive user.") else: return HttpResponseRedirect(settings.LOGIN_URL) return render(request, "index.html")
登出
def Logout(request): logout(request) return HttpResponseRedirect(settings.LOGIN_URL)
有人可以帮我把这些视图转换成Class Based Views
Django吗?我对这些东西很陌生,并且无法正确理解它们究竟是如何工作的.将不胜感激任何帮助!
浏览文档https://docs.djangoproject.com/en/1.8/topics/class-based-views/intro/#using-class-based-views
from django.views.generic import View class LoginView(View): def post(self, request): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) return HttpResponseRedirect('/form') else: return HttpResponse("Inactive user.") else: return HttpResponseRedirect(settings.LOGIN_URL) return render(request, "index.html") class LogoutView(View): def get(self, request): logout(request) return HttpResponseRedirect(settings.LOGIN_URL)