Q. Which middleware is responsible for CSRF protection?

  • (A) SecurityMiddleware
  • (B) CSRFMiddleware
  • (C) CsrfViewMiddleware
  • (D) SessionMiddleware
πŸ’¬ Discuss
βœ… Correct Answer: (C) CsrfViewMiddleware
Explanation: `CsrfViewMiddleware` enables CSRF protection in Django.

Q. Which of the following is used to return a 404 error in Django?

  • (A) Http404
  • (B) HttpError
  • (C) PageNotFound
  • (D) Error404
πŸ’¬ Discuss
βœ… Correct Answer: (A) Http404
Explanation: You can raise `Http404` from `django.http` to return a 404 error.

Q. Which setting is used to enable debugging in Django?

  • (A) DEBUG = False
  • (B) DEBUG = True
  • (C) LOGGING = True
  • (D) VERBOSE = True
πŸ’¬ Discuss
βœ… Correct Answer: (B) DEBUG = True
Explanation: Setting `DEBUG = True` in `settings.py` enables debugging in Django.

Q. Which function is used to redirect to another URL in Django?

  • (A) send()
  • (B) redirect()
  • (C) navigate()
  • (D) url_return()
πŸ’¬ Discuss
βœ… Correct Answer: (B) redirect()
Explanation: The `redirect()` function is used to redirect users to another URL.

Q. How are URL parameters captured in Django?

  • (A) With curly braces {}
  • (B) With square brackets []
  • (C) With angle brackets <>
  • (D) With parentheses ()
πŸ’¬ Discuss
βœ… Correct Answer: (C) With angle brackets <>
Explanation: Django uses angle brackets `<parameter>` to capture URL parameters in path().

Q. Which method is used to retrieve a single object or raise a 404 error if not found?

  • (A) get_object()
  • (B) object_or_404()
  • (C) get_or_404()
  • (D) get_object_or_404()
πŸ’¬ Discuss
βœ… Correct Answer: (D) get_object_or_404()
Explanation: `get_object_or_404()` is a shortcut that retrieves an object or raises an Http404 exception.

Q. Which of the following is used to define the location of static files?

  • (A) STATIC_DIR
  • (B) STATIC_ROOT
  • (C) STATICFILES_DIRS
  • (D) STATIC_PATH
πŸ’¬ Discuss
βœ… Correct Answer: (C) STATICFILES_DIRS
Explanation: `STATICFILES_DIRS` defines additional locations the staticfiles app will traverse to collect static files.

Q. What does the `reverse()` function in Django do?

  • (A) Reverse a string
  • (B) Reverse a queryset
  • (C) Return a URL path by its view name
  • (D) Reverse the order of a list
πŸ’¬ Discuss
βœ… Correct Answer: (C) Return a URL path by its view name
Explanation: `reverse()` returns the URL of a view by using its name.

Q. What does the `Meta` class inside a model do?

  • (A) Defines views
  • (B) Specifies metadata like ordering or verbose name
  • (C) Stores admin credentials
  • (D) Connects the model to a view
πŸ’¬ Discuss
βœ… Correct Answer: (B) Specifies metadata like ordering or verbose name
Explanation: The `Meta` class defines metadata options for a model such as ordering and verbose_name.

Q. Which command shows the SQL statements for migrations?

  • (A) python manage.py sql
  • (B) python manage.py sqlmigrate
  • (C) python manage.py showmigrate
  • (D) python manage.py makemigrate
πŸ’¬ Discuss
βœ… Correct Answer: (B) python manage.py sqlmigrate
Explanation: `sqlmigrate` shows the SQL that would be run for a migration.