Django - Built In Authentication Flashcards
To use Django but in authentication to login and logout
On urls.py
Include (django.contrib.auth.urls)
On templates
Create registration dir
With login.html and logout.html
Create form, no need for view, Django has created one for you
Specify where Django will go once you have logged in or out
on settings:
LOGIN_REDIRECT_URL = ‘home’
LOGOUT_REDIRECT_URL = ‘home’
NB: no need to create a view or model, Django has done that for you
Get authenticated user on Template
{% if user.is_authenticated%}
Code Logic For authenticated User
{% else %}
Code logic For unauthenticated user
{% endif %}
For sign up we need to create our own view
Django provides a class UserCreationForm to make this easy
ImportUserCreationForm from Django.contrib.auth.forms
Create class that inherits from User mCreationForm
class SignUpView( UserCreationForm):
form_class = UserCreationForm
template_ name =
success_url = reverse_lazy(‘url name’)
Why reverse lazy?
The reason is that for all generic class-based views the URLs are not loaded when the file is imported, so we have to use the lazy form of reverse to load them later when they’re available.