Django unit test response context is None

后端 未结 2 1401
小蘑菇
小蘑菇 2021-01-23 10:44

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

2条回答
  •  生来不讨喜
    2021-01-23 11:06

    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

    • Testing responses
    • context_data

提交回复
热议问题