0
0
Djangoframework~10 mins

CSRF protection mechanism in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to include CSRF protection in a Django template form.

Django
<form method="post">\n  [1]\n  <input type="submit" value="Submit">\n</form>
Drag options to blanks, or click blank then click option'
A{% csrf_field %}
B{% csrf_protect %}
C{% csrf_token %}
D{% csrf_input %}
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect template tags like {% csrf_protect %} inside the form.
Forgetting to include the CSRF token tag inside POST forms.
2fill in blank
medium

Complete the Django view decorator to enable CSRF protection.

Django
from django.views.decorators.csrf import [1]\n\n@[1]\ndef my_view(request):\n    # view code here\n    pass
Drag options to blanks, or click blank then click option'
Acsrf_protect
Bcsrf_token
Ccsrf_exempt
Dcsrf_check
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'csrf_exempt' which disables CSRF protection.
Confusing the decorator with the template tag.
3fill in blank
hard

Fix the error in the middleware setting to enable CSRF protection.

Django
MIDDLEWARE = [\n    'django.middleware.security.SecurityMiddleware',\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    [1],\n    'django.middleware.common.CommonMiddleware',\n]
Drag options to blanks, or click blank then click option'
A'django.middleware.csrf.CsrfMiddleware'
B'django.middleware.csrf.CsrfViewMiddleware'
C'django.middleware.security.CsrfViewMiddleware'
D'django.middleware.csrf.CsrfProtectionMiddleware'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect middleware class names that do not exist.
Placing the middleware in the wrong order.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters POST data keys starting with 'csrf'.

Django
csrf_data = {key: value for key, value in request.POST.items() if key.[1]('csrf') and value [2] ''}
Drag options to blanks, or click blank then click option'
Astartswith
Bendswith
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'endswith' instead of 'startswith' for keys.
Using '==' instead of '!=' to filter values.
5fill in blank
hard

Fill all three blanks to create a safe form submission check in a Django view.

Django
if request.method == '[1]' and request.POST.get('[2]') == '[3]':\n    # process form data
Drag options to blanks, or click blank then click option'
APOST
Bcsrfmiddlewaretoken
Cvalid_token
DGET
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'GET' method instead of 'POST'.
Using wrong CSRF token field name.
Comparing token to wrong value.