Would writing a middleware for this be a good idea? Then if we get the
DNE, we just return 404 from process_exception. And during development
we can just keep our middleware out, so that no real bugs beome 404.
When we deploy, we can just switch this middleware on.

On Sep 20, 9:12 pm, Forest Bond <[EMAIL PROTECTED]> wrote:
> Hi,
>
> On Thu, Sep 20, 2007 at 08:47:57AM -0700, shabda wrote:
> > I have a number of pages which can potentially raise the
> > self.model.DoesNotExist exception. Whenever this exception is raised,
> > it maps to a HTTP 404 for me. (That object does not exist in the
> > database, so I cant show this, so I want to return a 404 exception).
> > So apart from manually adding try-except to all my views, how can I
> > return a 404 response on the self.model.DoesNotExist.
>
> You probably don't want to do this in your model.  How about:
>
> from django.core.exceptions import ObjectDoesNotExist
> from django.core.http import Http404
>
> def 404_on_dne(wrapped):
>     def wrapper(*args, **kwargs):
>         try:
>             return wrapped(*args, **kwargs)
>         except ObjectDoesNotExist:
>             raise Http404
>     return wrapper
>
> Then you can wrap your views:
>
> @404_on_dne
> def my_view(request, arg1, arg2):
>    obj = Model.objects.get(...)
>    # do something
>
> I'm not entirely sure that you shouldn't just explicitly handle these 
> exceptions
> in your views (you can end up masking some DNE exceptions that are really 
> bugs),
> but that's a way to do what you want.
>
> Disclaimer: I haven't tried any of this code.  Might have bugs.
>
> -Forest
> --
> Forest Bondhttp://www.alittletooquiet.net
>
>  signature.asc
> 1KDownload


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to