L07-Django Models & Databases
L07-Django Models & Databases
Autotester
● Ignore color, do font size that makes sense for you
Booking system
● Fixed…
Django’s default database backend
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3’,
'NAME': BASE_DIR / 'db.sqlite3’,
}
}
https://docs.djangoproject.com/en/4.2/ref/models/querysets/
QuerySets
QuerySets can be chained
● Results must satisfy all conditions (same effect as the AND operator)
● Useful for applying multiple filters and sort orders
Authorization
Determines a user’s access right
Checks user’s privilege level (group) and permissions
User authentication in Django
User:Derived class of AbstractUser
Contains predefined fields: username, firstname, lastname, email, etc.
Passwords are hashed before they are stored
Passwords are also salted before hashing
● Prevents a rainbow table attack
● Salt is a random value that is added to the password
Authentication
Clients should tell the server who they are
Can use Authorization header in HTTP
Several authentication methods available
● Password auth
● Session auth
● Token auth
Basic
Password authentication
Sends username and password for every request
● No concept of login and logout
https://sherryhsu.medium.com/session-vs-token-based-authentication-11a6c5ac45e4
Django session authentication
Checks that username/password combination is correct
user =authenticate(username='john',password='secret’)
Attaches user to the current session
login(request, user)
Django does the session id lookup internally
● User object is attached to the request object (request.user)
● User type is AnonymousUser if current visitor is not authenticated
Reproduction and/or sharing of course materials is prohibited. Course materials include lecture slides, course notes,
assignments, data and documents provided by the instructors. Course materials created by the instructors of this course
are their intellectual properties. They may not be shared, posted, rehosted, sold, or otherwise distributed and/or
modified without expressed permission from the authors. All such reproduction or dissemination is an infringement of
copyright and is prohibited. All rights are reserved by the instructors.