Created
August 7, 2018 09:48
-
-
Save martin-martin/577dfcd5db83f9fd755bde5a00cbb409 to your computer and use it in GitHub Desktop.
working authentication with Django 2.1 (including some gotchas!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render, redirect | |
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm | |
from django.contrib.auth import login, authenticate, logout | |
# Create your views here. | |
def signup_user(request): | |
if request.method == 'POST': | |
form = UserCreationForm(request.POST) | |
if form.is_valid(): # first see whether all is good | |
form.save() # at this point the new user is created | |
# now we want to log them in right away, for UX reasons | |
# therefore we'll fetch username and pwd from the form | |
username = form.cleaned_data.get('username') | |
# ATTENTION: it's important to fetch 'password1' (which is | |
# the original password) instead of just 'password', which | |
# does not exist! However, .get() will not fail with an | |
# exception if it doesn't find the key in the dictionary! | |
password = form.cleaned_data.get('password1') | |
user = authenticate(username=username, password=password) | |
login(request, user) | |
return redirect('/home') | |
else: | |
# if the user is not submitting, create an empty form | |
# this runs the first time the user accesses the site | |
form = UserCreationForm() | |
return render(request, 'signup.html', {'form': form}) | |
def login_user(request): | |
if request.method == 'POST': | |
# It's really important to remember to put `data=request.POST` | |
# The AuthenticationForm works a little different than the other | |
# forms, so the request is not its default first input... | |
# messy - but that's how it is! | |
form = AuthenticationForm(data=request.POST) | |
if form.is_valid(): | |
username = form.cleaned_data.get('username') | |
password = form.cleaned_data.get('password') | |
user = authenticate(username=username, password=password) | |
login(request, user) | |
return redirect('/home') | |
else: | |
form = AuthenticationForm() | |
return render(request, 'login.html', {'form': form}) | |
def logout_user(request): | |
logout(request) | |
return redirect('/login') | |
def home(request): | |
return render(request, 'home.html', {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment