# detailView and http response not working

**URL:** <https://forum.djangoproject.com/t/detailview-and-http-response-not-working/29460>\
**Category:** Mystery Errors\
**Created:** [March 25, 2024, 1:16pm UTC](https://forum.djangoproject.com/t/detailview-and-http-response-not-working/29460 "2024-03-25T13:16:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luigiamorfini1982](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/luigiamorfini1982/32/8836_2.png) [@luigiamorfini1982](https://forum.djangoproject.com/u/luigiamorfini1982)\
**Post date:** [March 25, 2024, 1:16pm UTC](https://forum.djangoproject.com/t/detailview-and-http-response-not-working/29460/1 "2024-03-25T13:16:37Z")

</div>

Hi my name Luigi born italy my english school … i’am studing english and python or django  
My problem httpresponse not working

idea ? Fix to problem?  
Thanks.

```auto
from django.http import HttpResponse

class Profile(braces.views.LoginRequiredMixin, generic.DetailView):
    model = User
    def get_object(self):
        if not User.objects.filter(username=self.kwargs['username']).exists():  
           return HttpResponse(self.request, "Error : data object not found")
        else:
            self.template_name = "user_profile.html"

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 25, 2024, 1:18pm UTC](https://forum.djangoproject.com/t/detailview-and-http-response-not-working/29460/2 "2024-03-25T13:18:58Z")

</div>

Whenever you are requesting assistance with an error, please post the complete error message with the traceback from the server console.

---

<div class="post-metadata">

**Author:** ![luigiamorfini1982](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/luigiamorfini1982/32/8836_2.png) [@luigiamorfini1982](https://forum.djangoproject.com/u/luigiamorfini1982)\
**Post date:** [March 25, 2024, 1:52pm UTC](https://forum.djangoproject.com/t/detailview-and-http-response-not-working/29460/3 "2024-03-25T13:52:24Z")

</div>

```auto
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 25, 2024 - 14:50:58
Django version 4.2.11, using settings 'config.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.

[25/Mar/2024 14:51:07] "GET /accounts/profile/luigi/ HTTP/1.1" 200 6732
[25/Mar/2024 14:51:08] "GET /static/client/ico/favicon.ico HTTP/1.1" 404 1825
[25/Mar/2024 14:51:17] "GET /accounts/profile/demo/ HTTP/1.1" 200 6732

```

my insert /demo/ … not showing message from httpresponse.  
no errors … no show message.

thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 25, 2024, 2:03pm UTC](https://forum.djangoproject.com/t/detailview-and-http-response-not-working/29460/4 "2024-03-25T14:03:54Z")

</div>

Ok, two things here:

> [@luigiamorfini1982](#):
>
> `self.template_name = "user_profile.html"`

This should be defined as a class attribute and not dynamically assigned in the get\_object method.

Second, this is not going to do what you think you want it to do:

> [@luigiamorfini1982](#):
>
> ```auto
> if not User.objects.filter(username=self.kwargs['username']).exists():  
> return HttpResponse(self.request, "Error : data object not found")
> 
> ```

A class-based view goes through a number of steps between when it is called and when a response is actually returned to the browser. What you are supposed to return from `get_object` is not an HttpResponse.

The two resources I recommend to people when trying to understand how CBVs work and how to use them is the [Classy Class-Based Views site](https://ccbv.co.uk/) and the [CBV diagrams](https://www.brennantymrak.com/articles/django-class-based-views-diagrams.html) page.

These are good additions to the docs at [Introduction to class-based views | Django documentation | Django](https://docs.djangoproject.com/en/5.0/topics/class-based-views/intro/) and the explanations and examples at [https://docs.djangoproject.com/en/5.0/intro/tutorial04/#use-generic-views-less-code-is-better](https://docs.djangoproject.com/en/5.0/intro/tutorial04/#use-generic-views-less-code-is-better).

---

<div class="post-metadata">

**Author:** ![luigiamorfini1982](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/luigiamorfini1982/32/8836_2.png) [@luigiamorfini1982](https://forum.djangoproject.com/u/luigiamorfini1982)\
**Post date:** [March 25, 2024, 3:35pm UTC](https://forum.djangoproject.com/t/detailview-and-http-response-not-working/29460/5 "2024-03-25T15:35:44Z")

</div>

ok, my using code:

views.py

```auto
class Profile(braces.views.LoginRequiredMixin, generic.DetailView):
    model = User
    def get_object(self):
        return get_object_or_404(User, username=self.kwargs['username'])

```

urls.py:

```auto
   path('profile/<str:username>/', views.Profile.as_view(template_name="user_profile.html"), name='user_profile'),

```

fixed… problem … How to custom message from get\_object\_or\_404??

thanks.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 25, 2024, 4:10pm UTC](https://forum.djangoproject.com/t/detailview-and-http-response-not-working/29460/6 "2024-03-25T16:10:30Z")

</div>

The short answer is that it’s not directly possible.

The docs for the shortcut function `get_object_or_404` state:

> Calls [`get()`](https://docs.djangoproject.com/en/5.0/ref/models/querysets/#django.db.models.query.QuerySet.get) on a given model manager, but it raises [`Http404`](https://docs.djangoproject.com/en/5.0/topics/http/views/#django.http.Http404) instead of the model’s [`DoesNotExist`](https://docs.djangoproject.com/en/5.0/ref/models/class/#django.db.models.Model.DoesNotExist) exception.

So then the [`Http404` exception](https://docs.djangoproject.com/en/5.0/topics/http/views/#django.http.Http404) is documented with:

> In order to show customized HTML when Django returns a 404, you can create an HTML template named `404.html` and place it in the top level of your template tree. This template will then be served when [`DEBUG`](https://docs.djangoproject.com/en/5.0/ref/settings/#std-setting-DEBUG) is set to `False` .

This means that all such errors will return the same page. (Note that there is no facility for passing a context to that template.)

Note that you do have more control over this process - see the docs for [Customizing error views](https://docs.djangoproject.com/en/5.0/topics/http/views/#customizing-error-views) and [handler404](https://docs.djangoproject.com/en/5.0/ref/urls/#handler404).

However, in reality, you may not want to actually raise the 404 exception. You might decide to just render a custom page, allowing Django to return a 200, but with a page indicating the invalid parameter. Or, you could just redirect the user to a different page.
