Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Django 404 handler


When visiting a page that does not exist, the site displays the 404 page, as expected.

However, when running unit tests to check that the 404 page is displayed, the test fails because the HTTP status code is 200 rather than 404.


def test_client_detail_view_with_invalid_customer(self):
self.client.login(username="test_user", password="password123")
response = self.client.get("/customer/123456/")
self.assertEqual(response.status_code, 404)



AssertionError: 200 != 404


The default handler that is set up in _keenthemes/urls.py appears to be configured with the correct status code:


handler404 = SystemView.as_view(template_name="pages/system/not-found.html", status=404)


How can I get this to work correctly in unit tests?

Regards,
Alan


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Hi Alan,

Your ClientListView function still returns a 200 status code.

Maybe you could put some conditions to check the user's existence.

If not found, then return this in the view function. By right, the 404 status code will trigger.


return HttpResponseNotFound("<h1>User not found</h1>")



Thanks


FYI 'Customers' has now been renamed to 'Clients' in order to fit with the company's Domain Language (although the problem still persists)...

When running in the browser, the 404 is returned correctly for invalid routes, but it does not work in unit tests. In unit tests the status code 200 is returned, even for invalid routes.

View:

    class ClientListView(LoginRequiredMixin, PermissionRequiredMixin, ListView):
        permission_required = 'sop.view_client'
        permission_denied_message = 'You do not have permission to view client information.'

        template_name = 'pages/sop/client_list.html'
        context_object_name = 'clients'
        model = Client

        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            context = KTLayout.init(context)
            return context

Url:

    path('clients', ClientListView.as_view(), name='client-list'),

Test:

    def test_client_detail_view_with_invalid_commercial_client(self):
        self.client.login(username="sop_user", password="P@55w0Rd")
        response = self.client.get("/sop/client/com/123456/")
        self.assertEqual(response.status_code, 404)

_keenthemes/urls.py:

    handler404 = SystemView.as_view(template_name='pages/system/not-found.html', status=404)
    handler500 = SystemView.as_view(template_name='pages/system/error.html', status=500)

Hi Alan,

Thank you for contacting us. We need to see your function that generates results for page "/customer/123456/". Even though the user with this id is not found, the page URL to "customer" is still valid.

If the customer is not found, you should return the 404 status.

The handler404 will work for undefined routes. Eg. "customerxyz/123"

Thanks


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(