Bird
0
0

Identify the error in this Django template snippet that causes CSRF protection to fail:

medium📝 Debug Q6 of 15
Django - Security Best Practices
Identify the error in this Django template snippet that causes CSRF protection to fail:
<form method='post'>
  <input type='hidden' name='csrfmiddlewaretoken' value='{{ csrf_token }}'>
  <button type='submit'>Send</button>
</form>
AThe CSRF token variable is not rendered correctly
BThe input name should be 'csrf_token' not 'csrfmiddlewaretoken'
CThe CSRF token must be added using {% csrf_token %} tag
DThe form method should be 'get' instead of 'post'
Step-by-Step Solution
Solution:
  1. Step 1: Check CSRF token insertion method

    Manually adding a hidden input with csrf_token variable is error-prone.
  2. Step 2: Use Django recommended way

    Django recommends using {% csrf_token %} template tag to insert the token properly.
  3. Final Answer:

    The CSRF token must be added using {% csrf_token %} tag -> Option C
  4. Quick Check:

    Use {% csrf_token %} tag for CSRF token [OK]
Quick Trick: Always use {% csrf_token %} tag in forms [OK]
Common Mistakes:
MISTAKES
  • Manually setting input value without tag
  • Using wrong input name
  • Changing form method to GET

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes