Source

importrefromdjango.confimportsettingsfromdjango.views.staticimportservefromdjango.shortcutsimportredirectfrom.exceptionsimportRedirectclassStaticServe(object):""" Django middleware for serving static files instead of using urls.py """regex=re.compile(r'^%s(?P<path>.*)$'%settings.MEDIA_URL)defprocess_request(self,request):ifsettings.DEBUG:match=self.regex.search(request.path)ifmatch:returnserve(request,match.group(1),settings.MEDIA_ROOT)classRedirectMiddleware(object):""" You must add this middleware to MIDDLEWARE_CLASSES list, to make work Redirect exception. All arguments passed to Redirect will be passed to django built in redirect function. """defprocess_exception(self,request,exception):ifnotisinstance(exception,Redirect):returnreturnredirect(*exception.args,**exception.kwargs)