JourneyJoker/profiles/views.py

36 lines
1.1 KiB
Python
Raw Normal View History

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
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("/")