Django HttResponse has no attr status_code, but docs have :)

Viewed 30

I'm using default django httpresponse, like this:

return HttpResponse("Validation Error", status_code=400)

And i got exception like "HttpResponse has no attribute status_code". But in the docs we have info about this attr (docs link)

If we look at source code, we can see:

class HttpResponse(HttpResponseBase):
    ...
    def __init__(self, content=b'', *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Content is a bytestring. See the `content` property methods.
        self.content = content
...

Not explict usage of status_code, let's look parent's init:

class HttpResponseBase:

    def __init__(self, content_type=None, status=None, reason=None, charset=None, headers=None):
...
        if status is not None:
            try:
                self.status_code = int(status)
            except (ValueError, TypeError):
                raise TypeError('HTTP status code must be an integer.')

            if not 100 <= self.status_code <= 599:
                raise ValueError('HTTP status code must be an integer from 100 to 599.')
        self._reason_phrase = reason

And here in params we can see status.

If we back to my example, correct version of this looks:

return HttpResponse("Validation Error", status=400)

And my question is: "Is there place in docs where was talk about status of HttpResponse and if not, how i can make issue or bring this info to django-developers?"

The way I see it, it's not obvious enough.

1 Answers

the status_code is an attribute of HttpResponse it means if you want to know what is the response status you can print your HttpResponse object status_code like this

resp = HttpResponse()
print(resp.status_code)

and in the document in this section they talk about methods and arguments

for example here they talk about status argument of HttpResponse

Related