axiacore

Blog

Django: How to send request to JSON view in tests

Vera Mazhuga

Vera Mazhuga

May 20, 2014

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.
Get in the loop and learn more about Axiacore:

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...

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...

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...

Camilo Nova

Camilo Nova

We build software solutions for companies from idea to launch.

Let's Talk