Auth & Security Questions with Answers

10 practice questions on Auth & Security (Django) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Easy

Which decorator restricts a function view to authenticated users?

  1. @auth_only
  2. @login_required
  3. @permission
  4. @private

Answer: B. @login_required

Explanation

Unauthenticated users are redirected to LOGIN_URL.

Step-by-step solution

  1. Add decorator.
  2. 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?

  1. login()
  2. verify()
  3. authenticate()
  4. check_user()

Answer: C. authenticate()

Explanation

Call authenticate first, then login(request, user).

Step-by-step solution

  1. authenticate.
  2. 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?

  1. It parameterizes queries instead of concatenating user input
  2. It rejects every request containing quote characters before the view is called
  3. It encrypts the SQL
  4. 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

  1. ORM builds params.
  2. 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?

  1. In cookies only, with the full session data serialized and signed in the browser
  2. In a text file
  3. In the database table django_session
  4. 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

  1. Cookie id.
  2. 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?

  1. User.objects.create_user(username, password=...)
  2. User.objects.create(password='x')
  3. User(password='x').save()
  4. 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

  1. Use create_user.
  2. 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?

  1. create_post, update_post, remove_post and read_post, written by the developer
  2. add_post, change_post, delete_post, view_post
  3. post_read
  4. 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

  1. Per model.
  2. 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?

  1. Checks the password against the stored hash and returns the matching user
  2. Creates the user
  3. Sends an email
  4. 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

  1. Session set.
  2. 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?

  1. DEBUG = False
  2. ALLOWED_HOSTS set to the real domain
  3. DEBUG = True
  4. 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

  1. Debug leaks info.
  2. 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?

  1. Encrypting the database connection between Django and the server it talks to
  2. Enabling TLS on runserver
  3. Hashing cookies
  4. 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

  1. HTTP request.
  2. 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?

  1. Encrypting stored passwords so that leaked database rows cannot be read directly
  2. Preventing SQL injection
  3. Blocking brute-force logins
  4. 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

  1. Attacker site posts.
  2. 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 →

Related topics

See all Django topics · All topics