How to show custom error with get_object_or_404

I would like to know how to show a personalized error with get_object_or_404?, I do not want regular http404 pages, I want to display a message with the result none

thank:)

+9
source share
4 answers

get_object_or_404()- Essentially simple 5 line function. If you have no specific reason for using it, simply do:

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

If for any reason you have to use get_object_or_404 (), you can try putting it in a block try: ... except Http404: ..., but I honestly can't come up with a plausible reason for this.

+16

, get_object_or_404 , http 404. , DEBUG, : "No MyModel ".

doc. : Model, * args ** kwargs. get() filter() .

, , , , , , get_object_or_404(), , , , Model.DoesNotExist.

. , , django.shortcuts [1].

+3

, ?
get_object_or_404() 404 ?
, , django 404 html .

+2

. Http404. - try catch, . ,

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

All Articles