Django - Built In Authentication Flashcards

1
Q

To use Django but in authentication to login and logout

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Get authenticated user on Template

A

{% if user.is_authenticated%}
Code Logic For authenticated User
{% else %}
Code logic For unauthenticated user
{% endif %}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

For sign up we need to create our own view
Django provides a class UserCreationForm to make this easy

A

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’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why reverse lazy?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly