views:

57

answers:

3

Hi guys, i would like to know how show personalized error with get_object_or_404?, i dont want the normal http404 pages, i want to display a message with the result is none

thanks :)

A: 

That cant be done with that shortcut. It can only raise a Http404 exception. Your best bet is a try catch if you want full control. Eg.

try:
    obj = Model.objects.get(pk = foo)
except:
    return HttpResponseRedirect('/no/foo/for/you')
    #or
    return render_to_response ...
michael
+5  A: 

The get_object_or_404() is essentially a simple 5-line function. Unless you have some specific reason for using it, just do:

try:
    instance = YourModel.objects.get(pk=something)
except YourModel.DoesNotExist:
    return render_to_response('a_template_with_your_error_message.html')

If for whatever reason you have to use get_object_or_404(), you can try putting it in a try: ... except Http404: ... block, but I honestly can't think of a plausible reason for that.

Aram Dulyan
A: 

And why exactly aren't you using your server's capeability to do just that?
get_object_or_404() is redirecting to the default 404 page right?
If you are on debug mode you won't see it but when deployed django will just refer to the server's 404 html page.

the_drow