from django.views.generic.edit import FormView from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from django.contrib.auth import login, authenticate, logout from django.shortcuts import redirect class RegistrationView(FormView): template_name = 'profiles/register.html' form_class = UserCreationForm success_url = '/?registered=true' def form_valid(self, form): res = super().form_valid(form) username = form.cleaned_data.get('username') password = form.cleaned_data.get('password1') user = authenticate(username=username, password=password) login(self.request, user) return res class LoginView(FormView): template_name = 'profiles/register.html' form_class = AuthenticationForm success_url = "/" def form_valid(self, form): res = super().form_valid(form) username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) login(self.request, user) return res def user_logout(request): logout(request) return redirect("/")