Reputation: 794
What is the proper way to test a class based Django view which returns a JsonResponse type. For example, the beginning of my class looks like this:
class MyView(LoginRequiredMixin, View):
def get(self, request):
return JsonResponse({
"status": 200,
"message": "message to return."
})
I have similar 'post' and 'delete' functions. The url /myview
is connected to this view. When I attempt something like this:
c = Client()
response = c.get('/myview')
'response' is a HttpResponsePermanentRedirect type, and I am unsure how the JSON data can be obtained from that.
EDIT -- I login in the setup of the test
Upvotes: 0
Views: 78
Reputation: 10389
I think you just need to add the trailing slash to your URL
c.get('/myview/')
if APPEND_SLASH
is turned on (and I think Django turns it on by default), any request without the trailing slash will generate a redirect to a URL with the trailing slash
Upvotes: 1