title: Django class-based view to return HttpResponse such as HttpResponseBadRequest
body: <p>I have a custom Django view class that inherits from the generic <code>DetailView</code>.</p>
  <p>The generic <code>DetailView</code> class sequentially calls its methods <code>get_queryset</code>, <code>get_object</code>, and others to generate an object to pass for a Django template.</p>
  <p>Moreover, the generic <code>DetailView</code> class raises <code>Http404</code> exception within these methods to deal with erroneous situations <a href="https://github.com/django/django/blob/main/django/views/generic/detail.py#L54" rel="nofollow noreferrer">#</a>.</p>
  <pre><code>except queryset.model.DoesNotExist:
      raise Http404(_(&quot;No %(verbose_name)s found matching the query&quot;) %
                    {'verbose_name': queryset.model._meta.verbose_name})
  </code></pre>
  <p>What I am trying to do is simple: I want to return other HTTP status codes to the client if an error is found within these methods. However, because <code>Http404</code> is the only Django <code>HttpResponse</code> that has a form of exception, it seems like this cannot be achieved easily.</p>
  <p>Because <code>HttpResponse</code> is not an exception, I have to return it instead of raising it. Therefore, the customized method becomes the following form.</p>
  <pre><code>def get_object(self, queryset=None):
     ...
     try:
        # Get the single item from the filtered queryset
        obj = queryset.get()
     except queryset.model.DoesNotExist:
        return HttpResponseBadRequest(&quot;Bad request.&quot;)
  </code></pre>
  <p>However, above code does not send <code>HttpResponseBadRequest</code> to the client. Instead, it returns <code>HttpResponseBadRequest</code> as an object for a Django template. The client gets 200 status code with an invalid object for the template.</p>
  <p>The only possible solution to think is writing some object-checking code within the Django template. However, I wonder if there is any better way to deal with this problem.</p>