0
0
Djangoframework~10 mins

Testing views with Client 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 create a test client instance in Django.

Django
from django.test import [1]

client = [1]()
Drag options to blanks, or click blank then click option'
ARequestFactory
BTestCase
CHttpRequest
DClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using TestCase instead of Client to create the client instance.
Importing RequestFactory which is different from Client.
2fill in blank
medium

Complete the code to send a GET request to the home page using the test client.

Django
response = client.[1]('/')
Drag options to blanks, or click blank then click option'
Apost
Bget
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get for a GET request.
Using delete or put which are for other HTTP methods.
3fill in blank
hard

Fix the error in the assertion to check the response status code is 200.

Django
assert response.status_code [1] 200
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which checks for inequality.
Using '>' or '<' which do not confirm exact status.
4fill in blank
hard

Fill both blanks to test a POST request with data and check the response status code.

Django
response = client.[1]('/submit/', data=[2])
assert response.status_code == 302
Drag options to blanks, or click blank then click option'
Apost
B{'name': 'Alice'}
C{'username': 'bob'}
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using get instead of post for sending data.
Passing data as a string instead of a dictionary.
5fill in blank
hard

Fill all three blanks to test a view that requires login, using the client to login and then access a protected page.

Django
client.[1](username='user', password='pass')
response = client.[2]('/dashboard/')
assert response.status_code [3] 200
Drag options to blanks, or click blank then click option'
Alogin
Bget
C==
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of login to authenticate.
Using post instead of get to access the page.
Using != instead of == in the assertion.