Can i rise 404 from my middleware ?

813 views
Skip to first unread message

Mirat Can Bayrak

unread,
Aug 4, 2009, 5:45:45 PM8/4/09
to django...@googlegroups.com
Question is simple. i tried to use raise Http404 from middleware and that is not working.

My middleware is :

import urlparse
from django.contrib.sites.models import Site
from django.http import Http404

class GetSubdomainMiddleware:
def process_request(self, request):
site = Site.objects.filter(domain__exact = request.META['HTTP_HOST'])
if site:
bits = urlparse.urlsplit(request.META['HTTP_HOST'])[2].split('.')
request.subdomain = bits[0]
else:
raise Http404

--
Mirat Can Bayrak <miratca...@gmail.com>

Alex Gaynor

unread,
Aug 4, 2009, 5:47:37 PM8/4/09
to django...@googlegroups.com
Right now there is a bug in django's middleware where it doesn't
correct catch exceptions: http://code.djangoproject.com/ticket/6094.

Alex

--
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me

Mirat Can Bayrak

unread,
Aug 5, 2009, 2:29:50 AM8/5/09
to django...@googlegroups.com
than... what should i do?

Malcolm Tredinnick

unread,
Aug 5, 2009, 2:50:02 AM8/5/09
to django...@googlegroups.com
On Wed, 2009-08-05 at 09:29 +0300, Mirat Can Bayrak wrote:
> On Tue, 4 Aug 2009 16:47:37 -0500
> Alex Gaynor <alex....@gmail.com> wrote:
[...]

> > Right now there is a bug in django's middleware where it doesn't
> > correct catch exceptions: http://code.djangoproject.com/ticket/6094.

> than... what should i do?

Don't raise an exception. A 404 exception is eventually turned into an
HttpResponse with the appropriate status code, so construct one manually
and return that from the middleware. Once you return a response, no
further processing happens (for request middleware). That's one of the
major points about middleware.

Regards,
Malcolm

Mirat Bayrak

unread,
Aug 7, 2009, 6:03:47 AM8/7/09
to django...@googlegroups.com
2009/8/5 Malcolm Tredinnick <mal...@pointy-stick.com>
Thank you, i did it the way you said. It's little weird but working, thank you.
Reply all
Reply to author
Forward
0 new messages