This simple middleware rewrites the 'href' attribute of any `<a>` tags in your response content. The URL href is modified by appending the string '.nyud.net', which causes the Coral Content Distribution Network to retrieve a copy of the page and cache it before returning it to the user agent.
This might be useful if you're writing another Slashdot and you want to avoid turning the servers you link to into smoking craters.
You [should be able](http://www.djangosnippets.org/snippets/910/) to apply this functionality to a single view as well (though I haven't tried this yet).
Rather than using the full GZipMiddleware, you may want to just compress some views. This decorator lets you do that.
@gzip_compress
def your_view(request, ...):
....
This is a simple middleware that redirects the exactly URL requested with the correct domain. It is useful when you have more than one domain (most of cases with "www." or IP) to access a website.
To make it works, download the snippet file as the name "permanent_redirect.py" and add its path as the first item in MIDDLEWARE_CLASSES setting in settings.py.
Later you must inform a setting called `HTTP_HOST_DOMAIN` with the correct domain.
While we're on the topic of SSLRedirect (See snippet 240 and 880) here's what I add to the end of my ssl middleware module, so that SSLRedirect wont break my automated testing.
It simply creates a dummy middleware class that removes the SSL argument, but does not do any redirecting. If you were to simply remove the middleware from settings, the extra SSL argument will then get passed on to all the relevant views.
Of course, you'll need to define a TESTING variable in settings, or change this to something else like settings.DEBUG. Having the separate variable for testing allows you to run your server in DEBUG mode without side effects like the change above.
Snippet 240 is great, but it does not handle flatpages since flatpages are not technically a view. This operates on the request level, not the view level so it will handle flat pages.
**Step 1** Add this class to your MIDDLEWARE_CLASSES
**Step 2** Add an entry in settings.py which is a list of regex to match against urls that u want to have ssl:
SSL_URLS = (
r'/login/',
r'/home/',
r'/super-sensitive-information/',
)
based on Snippet [799](http://www.djangosnippets.org/snippets/799/) but added code inspection capabilities. Credit goes to django-logging for the actual inspection code.
Got the idea from the [This Week in Django](http://blog.michaeltrier.com/2008/6/18/this-week-in-django-26-2008-06-16) Podcast ;)
This adds the filename, lineno, functionname and the actual python-code line which caused a sql statement to be executed.
Note that i am adding keys to 'connection.queries' dict on request, which may be dangerous, so use with care!
The code inspection functionality can be toggled via FRAME_INSPECT.
Handles exceptions from AJAX code, giving more useful errors and tracebacks.
(By the way, all these new snippets are extracts from [django-webapp](http://code.google.com/p/django-webapp/).)
This exact code is not well tested, but it's refactored from some code we use in production.
Based on Snippet [766](http://www.djangosnippets.org/snippets/766/) but added syntax highlighting of the sql output via [pygments](http://pygments.org/). The sql output will also be wrapped at 120 characters by default (can be configured by changing WRAP). It degrades nicely if pygments is not installed. This will add quite some cpu-cycles just for printing debug messages so use with care.
Following is the rest of the original description by [simon](http://www.djangosnippets.org/users/simon/) (shamelessly copied):
Adds a hidden footer to the bottom of every text/html page containing a list of SQL queries executed and templates that were loaded (including their full filesystem path to help debug complex template loading scenarios).
To use, drop into a file called 'debug_middleware.py' on your Python path and add 'debug_middleware.DebugFooter' to your MIDDLEWARE_CLASSES setting.
Edit: Added the ability to set the height of the debug-box
If you feel nostalgic for the old days of crufty URLs, put this middleware somewhere in your Django app and add the first entry in settings.MIDDLEWARE_CLASSES as shown below. Keep in mind that you need to replace 'path.to' with the correct Python path to the middleware.
MIDDLEWARE_CLASSES = (
'path.to.RandomFileExtensionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
)
Note: Although this works it's still a joke.
Activate this middleware and define `LOG_FORMAT` & `LOG_ROOTS` in your `settings.py`. Then you can use Python's `logging` module for easy logging within your application.
Adds a hidden footer to the bottom of every text/html page containing a list of SQL queries executed and templates that were loaded (including their full filesystem path to help debug complex template loading scenarios).
To use, drop in to a file called 'debug_middleware.py' on your Python path and add 'debug_middleware.DebugFooter' to your MIDDLEWARE_CLASSES setting.
I needed a quick and dirty way to block a user from my site.
Just include this middleware class under the 'MIDDLEWARE_CLASSES' variable in your settings.py file.
Also include the variable BLOCKED_IPS = ('123.123.123.123',) variable, where the value is a tuple of IP addresses you want blocked from your site.
<code>
is_loaded = False
if not is_loaded:
is_loaded = True
#... ExceptionHandlingMiddleware as EHM
import views
EHM.append(views.AuthFailError, views.auth_fail) # when AuthFailError thrown it redirects to `auth_fail` view function.
</code>