Insights

Django: How to send request to JSON view in tests

Photo of the author: Vera Mazhuga

Vera Mazhuga

  •  

1 min read.

If in your Django project you have a view that accepts or returns JSON, you can use the following code for your unittests

import json
from django.test import TestCase

class JSONViewTestCase(TestCase):
    def test_json_view(self):
        response = self.client.post(
            reverse(
                'my_json_view',
                args=('test', 123)
            ),
            json.dumps({
                'user': 'me@example.com',
            }),
            'json',
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
        )
        json_string = response.content
        response_data = json.loads(json_string)
        # do smth.

Learn more by receiving an email once a month.

Additional Insights

OSX Maveriks compiler error installing pip package

If you get this error:$ pip install lmxl...clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument...

Author Camilo Nova Camilo Nova

Atom editor for Debian

When Github announced that they made an editor and at the office and Pablo brought it for the first time in its invitation pe...

Photo of the author: Igor Támara Igor Támara

Simple jquery image lazy load

Sometimes you want to speed up the loading time of a web page, when you have a lot of images it can be useful to load them a...

Photo of the author: Camilo Nova Camilo Nova