37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
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 = "/"
|
|
|
|
def form_valid(self, form):
|
|
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 redirect(self.request.GET.get("next", "/") + "?registered=true")
|
|
|
|
class LoginView(FormView):
|
|
template_name = 'profiles/register.html'
|
|
form_class = AuthenticationForm
|
|
success_url = "/"
|
|
|
|
def form_valid(self, form):
|
|
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 redirect(self.request.GET.get("next", "/"))
|
|
|
|
def user_logout(request):
|
|
logout(request)
|
|
return redirect("/")
|
|
|