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.
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"
Whenever you are requesting assistance with an error, please post the complete error message with the traceback from the server console.
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
Ok, two things here:
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:
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 and the CBV diagrams page.
These are good additions to the docs at Introduction to class-based views | Django documentation | Django and the explanations and examples at https://docs.djangoproject.com/en/5.0/intro/tutorial04/#use-generic-views-less-code-is-better.
ok, my using code:
views.py
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:
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.
The short answer is that it’s not directly possible.
The docs for the shortcut function get_object_or_404
state:
Calls get()
on a given model manager, but it raises Http404
instead of the model’s DoesNotExist
exception.
So then the Http404
exception 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
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 and 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.