Search code examples
pythondjangodjango-testingdjango-tests

How to get html from web page in Django


I was trying to implement some testing in Django, and I needed to check the data on the web page with a string, but I couldn't retrieve data from the web page. so, how can I get data(html) from web page?

my tests.py

from rest_framework.test import APITestCase

class TicketTestCase(APITestCase):

    def test_user_details_on_ticket_id(self):

        response = self.client.get(f"/api/ticket/1")
        print(response.content) # this returns empty data in bytes

        # I want to check this
        #self.assertEquals(response.data, "helloworld")

Solution

  • What you're doing is correct. You can retrieve data from web page by using response.content . The problem is that you're missing a slash at the end of the url, so the url should be

     response = self.client.get(f"/api/ticket/1/")
    

    instead of

     response = self.client.get(f"/api/ticket/1")