Question:
How to test the post-Django function?

You have to use the below written codes to test the post-Django function:


class TaskURLTests(TestCase):

    @classmethod

    def setUpClass(cls):

        super().setUpClass()


    def setUp(self):

        self.guest_client = Client()


def test_url(self):

     json_data = {

         "model": "X5",

         "version": "LT",

         ...

     }

     response  = self.guest_client.post('/add_robot/',

                                        data= json.dumps(json_data),

                                        content_type='application/json')


Using json.dumps will convert the Python dictionary to JSON string and by setting the content_type as application/json you're specifying the format you're sending.


Answered by:> Meaningful Life

Credit:> StackOverflow


Blog Links: 

>Sorting the restframework in Django

>Ways to access instances of models in view in order to save both forms at once in Django

>What makes index.html have such kind of name in Django?

>Fix Module Not Found during Deployment- Django

>Creating a Django with existing directories, files, etc.?

>How to Read a CSV file with PHP using cURL?



Nisha Patel

Nisha Patel

Submit
0 Answers