Sample questions
Question 1Easy
Which function verifies a username and password against the configured backends?
- authenticate()
- login()
- verify_user()
- check_password()
Answer: A. authenticate()
Explanation
authenticate returns a User or None; login then starts the session.
Step-by-step solution
- authenticate() checks username and password against backends.
- It returns a User object if valid, else None.
- login() is used after authentication to start a session.
- verify_user() and check_password() are not standard functions for this purpose.
- The correct function is authenticate().
Why the other options are wrong
- B. login() starts a session for a user you already have; it does not check the password.
- C. verify_user() is not a Django auth function; authenticate() is the function that checks credentials.
- D. check_password() checks one hash for a user; it does not try the configured backends.
Tip: Remember, authenticate() is the standard function for username and password verification.
Question 2Easy
Which setting lists the authentication backends that Django tries, in order, when authenticate() is called?
- AUTHENTICATION_BACKENDS
- AUTH_BACKENDS
- LOGIN_BACKENDS
- USER_AUTH_CLASSES
Answer: A. AUTHENTICATION_BACKENDS
Explanation
AUTHENTICATION_BACKENDS is a list of backend class paths. authenticate() asks each backend in turn, and the first one that accepts the credentials returns the user.
Step-by-step solution
- authenticate() reads the backend list from settings.
- It calls each backend's authenticate() in order.
- The first backend that returns a user wins.
Why the other options are wrong
- B. Not a Django setting; the name is AUTHENTICATION_BACKENDS.
- C. Not a Django setting; login does not choose backends.
- D. Not a Django setting; the name is AUTHENTICATION_BACKENDS.
Tip: Setting names end in _BACKENDS for this one; check the settings reference.
Question 3Easy
A view decorated with @login_required receives a request from an anonymous visitor. Where does Django redirect them by default?
- To the home page at /
- To settings.LOGIN_URL, with the original path in the
next query parameter - To a 403 Forbidden page
- To the admin site login
Answer: B. To settings.LOGIN_URL, with the original path in the next query parameter
Explanation
The decorator sends an unauthenticated user to LOGIN_URL and adds ?next= with the path they asked for, so the login view can send them back after signing in. It does not return 403 by default.
Step-by-step solution
- The decorator checks whether request.user is authenticated.
- If the user is anonymous, it redirects to settings.LOGIN_URL.
- It adds next= with the requested path, so the user returns there after login.
Why the other options are wrong
- A. login_required does not redirect to / ; it uses LOGIN_URL.
- C. login_required redirects; a 403 is what a failed permission check returns.
- D. The admin login is separate; LOGIN_URL decides where anonymous users go.
Tip: The decorator's redirect target is set by LOGIN_URL.
Question 4Medium
Which code stores a new password for a user so that Django keeps it as a hash?
- user.password = raw_password followed by user.save()
- user.hash_password(raw_password) followed by user.save()
- user.password_plain = raw_password followed by user.save()
- user.set_password(raw_password) followed by user.save()
Answer: D. user.set_password(raw_password) followed by user.save()
Explanation
set_password() runs the raw password through the configured hasher and stores the result. Assigning the raw string to the password field stores plain text, which check_password() will never match.
Step-by-step solution
- A raw string is not a hash.
- set_password() hashes it with the configured hasher.
- save() writes the hash to the database.
Why the other options are wrong
- A. That stores plain text; Django expects a hash in the password field.
- B. There is no such method on the user model.
- C. There is no such field; the password is stored in the password column.
Tip: Always use set_password(); never assign to user.password directly.
Question 5Medium
How does Django store user passwords by default?
- As plain text, so they can be shown on request
- As reversible encrypted text with a secret key
- As salted hashes from a configurable hasher
- As base64 strings of the original password
Answer: C. As salted hashes from a configurable hasher
Explanation
Passwords are hashed, never stored raw.
Step-by-step solution
- Django uses hashing to store passwords securely.
- Hashing makes passwords irreversible.
- Passwords are salted to prevent attacks.
- A configurable hasher is used for flexibility.
- Final answer: As salted hashes from a configurable hasher.
Why the other options are wrong
- A. Plain text would be a security failure; Django never stores passwords that way by default.
- B. Reversible encryption with a secret key is not how Django stores passwords; it uses one-way hashes.
- D. Base64 is an encoding that anyone can reverse, so it is not used to protect passwords.
Question 6Medium
Which decorator restricts a function view to logged-in users?
- @auth_only
- @require_login
- @permission
- @login_required
Answer: D. @login_required
Explanation
It redirects anonymous users to the login URL.
Step-by-step solution
- The question asks which decorator restricts a function view to logged-in users.
- @login_required is the standard Django decorator for this purpose.
- It checks if the user is authenticated before allowing access to the view.
- Other options like @auth_only or @require_login are not standard in Django.
- The correct answer is @login_required as it enforces login for views.
- Final Answer: D (@login_required).
Why the other options are wrong
- A. auth_only is not a Django decorator; login_required is the built-in one.
- B. require_login is not a built-in Django decorator; the built-in one is login_required.
- C. permission does not check login by itself; permission_required checks for a permission.
Tip: Remember @login_required is the default way to restrict views to logged-in users in Django.
Question 7Medium
What does django.contrib.auth.login(request, user) do?
- It checks the password against the stored hash and returns True or False, depending on whether the credentials match
- It creates a new User row in the database
- It stores the user's primary key in the session, so later requests are authenticated as that user
- It sends a password reset email
Answer: C. It stores the user's primary key in the session, so later requests are authenticated as that user
Explanation
login() records the user in the session, so AuthenticationMiddleware sets request.user for later requests. It does not check the password; authenticate() does that before login() is called.
Step-by-step solution
- authenticate() checks the credentials first.
- login() stores the user's id in the session.
- On later requests, AuthenticationMiddleware loads that user into request.user.
Why the other options are wrong
- A. That is authenticate(); login() trusts that the user is already verified.
- B. login() never writes to the users table.
- D. Password reset is a separate view and flow.
Tip: authenticate() checks who you are; login() remembers it.
Question 8Medium
In a template, which context variable (from the auth context processor) refers to the current user?
- current_user
- request_user
- user
- auth
Answer: C. user
Explanation
{{ user.is_authenticated }} is commonly used.
Step-by-step solution
- The auth context processor adds the current user to the template context.
- The variable is named user.
- So {{ user.is_authenticated }} works, and the answer is option C.
Why the other options are wrong
- A. Current_user is not the context variable name.
- B. Request_user is not set by the auth context processor.
- D. Auth is not the name of the user variable.
Tip: In templates, check {{ user.is_authenticated }} before showing private content.
Question 9Hard
A project needs a custom user model. When should AUTH_USER_MODEL be set?
- Before the first migration that creates the user table, because changing it later is difficult
- After the first migrate, using a data migration
- At any time, because Django rebuilds the users table automatically on each migrate and never locks in the setting
- Inside a view, before the first request
Answer: A. Before the first migration that creates the user table, because changing it later is difficult
Explanation
Other apps' migrations, such as the admin, create foreign keys to the user model. Switching it after those tables exist changes those keys, which is hard, so set it before the first migrate.
Step-by-step solution
- Other apps' migrations refer to the user model by foreign key.
- Switching it later changes those keys and needs manual work.
- So AUTH_USER_MODEL is set before the first migrate.
Why the other options are wrong
- B. Other apps already have foreign keys to the default user table, so switching later is difficult.
- C. Django does not rebuild the users table automatically; existing keys would need changing.
- D. AUTH_USER_MODEL is a settings value read at startup, not per request.
Tip: Decide the user model before the first migrate.
Practise all 9 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →