Skip to content Skip to sidebar Skip to footer

Django Unit Test Response Context Is None

Why is response.context None in the following tests? I've checked response.content and it is ''. if I remove the assertIsNotNone line I get a TypeError: 'NoneType' object is not s

Solution 1:

Quick workaround

There is another member dict of Context class (derived from SimpleTemplateResponse) which is called context_data. Using this instead of just context will give the expected context dict (django 1.9). But be aware, it's defined to be deprecated since django 1.8. So use it at your own risk. Unfortunately I didn't research a proper alternative until now.

Background

The existence of the context attribute of a response seems to be dependent on the type of template engine (templates) you are using. If you go for another engine than the one coming with django (default) you likely find the dict at the context_data attribute

References

Solution 2:

I was having this issue and to fix it did the following in the setUp function:

from django.test.utils import setup_test_environment
setup_test_environment()

Post a Comment for "Django Unit Test Response Context Is None"