2020-01-22 17:32:13 +00:00
|
|
|
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
|
2020-01-26 10:53:25 +00:00
|
|
|
success_url = "/"
|
2020-01-22 17:32:13 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
2020-01-26 10:53:25 +00:00
|
|
|
super().form_valid(form)
|
2020-01-22 17:32:13 +00:00
|
|
|
username = form.cleaned_data.get('username')
|
|
|
|
password = form.cleaned_data.get('password1')
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
login(self.request, user)
|
2020-01-26 10:53:25 +00:00
|
|
|
|
|
|
|
return redirect(self.request.GET.get("next", "/") + "?registered=true")
|
2020-01-22 17:32:13 +00:00
|
|
|
|
|
|
|
class LoginView(FormView):
|
|
|
|
template_name = 'profiles/register.html'
|
|
|
|
form_class = AuthenticationForm
|
|
|
|
success_url = "/"
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2020-01-26 10:53:25 +00:00
|
|
|
super().form_valid(form)
|
2020-01-22 17:32:13 +00:00
|
|
|
username = form.cleaned_data.get('username')
|
|
|
|
password = form.cleaned_data.get('password')
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
login(self.request, user)
|
2020-01-26 10:53:25 +00:00
|
|
|
|
|
|
|
return redirect(self.request.GET.get("next", "/"))
|
2020-01-22 17:32:13 +00:00
|
|
|
|
|
|
|
def user_logout(request):
|
|
|
|
logout(request)
|
|
|
|
return redirect("/")
|
|
|
|
|