Sample questions
Question 1Easy
Which decorator restricts a function view to authenticated users?
- @auth_only
- @login_required
- @permission
- @private
Answer: B. @login_required
Explanation
Unauthenticated users are redirected to LOGIN_URL.
Step-by-step solution
- Add decorator.
- Redirect anon.
Why the other options are wrong
- A.
@auth_only is not a Django decorator; the correct choice is @login_required. - C. There is no @permission decorator for this; login_required restricts a view to logged-in users.
- D.
@private is not a Django decorator; the correct choice is @login_required.
Question 2Easy
Which function checks credentials and returns a user or None?
- login()
- verify()
- authenticate()
- check_user()
Answer: C. authenticate()
Explanation
Call authenticate first, then login(request, user).
Step-by-step solution
- authenticate.
- login.
Why the other options are wrong
- A. login starts the session.
- B.
verify() is not a real function; the correct choice is authenticate(). - D.
check_user() is not a real function; the correct choice is authenticate().
Question 3Medium
How does the Django ORM help prevent SQL injection?
- It parameterizes queries instead of concatenating user input
- It rejects every request containing quote characters before the view is called
- It encrypts the SQL
- It disables raw SQL
Answer: A. It parameterizes queries instead of concatenating user input
Explanation
Raw SQL should also use placeholders: raw(sql, [param]).
Step-by-step solution
- ORM builds params.
- DB driver escapes.
Why the other options are wrong
- B. Quotes are allowed.
- C. Not encrypted.
- D. Raw is still available.
Question 4Medium
Where does Django store session data by default?
- In cookies only, with the full session data serialized and signed in the browser
- In a text file
- In the database table django_session
- In memory only
Answer: C. In the database table django_session
Explanation
The browser holds a session id cookie; data stays server side.
Step-by-step solution
- Cookie id.
- DB row.
Why the other options are wrong
- A. Cookie holds an id only.
- B. File backend is optional.
- D. Memory is not default.
Question 5Medium
Which method should you use to create a user so the password is hashed?
- User.objects.create_user(username, password=...)
- User.objects.create(password='x')
- User(password='x').save()
- user.password = 'x'
Answer: A. User.objects.create_user(username, password=...)
Explanation
create_user and set_password() hash with the configured hasher.
Step-by-step solution
- Use create_user.
- Hash stored.
Why the other options are wrong
- B. Stores raw text.
- C. Stores raw text.
- D. Stores raw text.
Question 6Medium
Which permission codename pattern is auto created for a Post model?
- create_post, update_post, remove_post and read_post, written by the developer
- add_post, change_post, delete_post, view_post
- post_read
- admin_post
Answer: B. add_post, change_post, delete_post, view_post
Explanation
Check with user.has_perm('blog.add_post').
Step-by-step solution
- Per model.
- Four default perms.
Why the other options are wrong
- A. Names are add and change.
- C. post_read is not the pattern; Django generates action_model names such as add_post.
- D. admin_post is not generated; the four default actions are add, change, delete and view.
Question 7Medium
What does login(request, user) do?
- Checks the password against the stored hash and returns the matching user
- Creates the user
- Sends an email
- Stores the user id in the session
Answer: D. Stores the user id in the session
Explanation
It rotates the session key and sets request.user on later requests.
Step-by-step solution
- Session set.
- User stays logged in.
Why the other options are wrong
- A. authenticate checks.
- B. create_user creates.
- C. No email.
Question 8Hard
Which setting is a standard security mistake in production?
- DEBUG = False
- ALLOWED_HOSTS set to the real domain
- DEBUG = True
- SECRET_KEY read from an environment variable
Answer: C. DEBUG = True
Explanation
Debug mode exposes settings and tracebacks; with DEBUG False you must set ALLOWED_HOSTS.
Step-by-step solution
- Debug leaks info.
- Disable in prod.
Why the other options are wrong
- A. It is the right setting.
- B. That is correct.
- D. That is recommended.
Question 9Hard
What is SECURE_SSL_REDIRECT = True for?
- Encrypting the database connection between Django and the server it talks to
- Enabling TLS on runserver
- Hashing cookies
- Redirecting all HTTP requests to HTTPS
Answer: D. Redirecting all HTTP requests to HTTPS
Explanation
Pair with SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE.
Step-by-step solution
- HTTP request.
- 301 to https.
Why the other options are wrong
- A. SECURE_SSL_REDIRECT concerns HTTP requests to the site, not the database connection.
- B. This setting redirects HTTP requests to HTTPS; it does not enable TLS on runserver.
- C. Cookies are not hashed by this setting; cookie flags come from SESSION_COOKIE_SECURE.
Question 10Hard
What is CSRF protection for?
- Encrypting stored passwords so that leaked database rows cannot be read directly
- Preventing SQL injection
- Blocking brute-force logins
- Blocking forged requests that a malicious site makes using the user's logged-in session
Answer: D. Blocking forged requests that a malicious site makes using the user's logged-in session
Explanation
The token proves the request came from your own form.
Step-by-step solution
- Attacker site posts.
- Token missing; rejected.
Why the other options are wrong
- A. Hashing does that.
- B. ORM parameterization does that.
- C. Rate limiting does that.
Practise all 10 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 →