Search engines might conclude there's duplicate content if `/some_view/` and `/some_view/?page=1` returns the same results. This middleware redirects `?page=1` to the URL without the page parameter. You can set the name of the parameter in settings.py as `PAGE_VAR`.
See [here](http://www.muhuk.com/2009/08/a-civilized-way-display-lots-of-data/) for more details.
On WebFaction, each host has it's own Apache instance, with WebFaction's main Apache instance forwarding requests. This is very useful but means that some of the original information is lost. This middleware should be installed at the top of your list to restore this lost info.
It includes the functionality that used to be in SetRemoteAddrFromForwardedFor before it was removed from Django.
When running the Django development server, this middleware causes all executed SQL queries to get printed out to the console.
This is based on [Snippet 161](http://www.djangosnippets.org/snippets/161/). The big difference is that 161 logs by adding stuff to your response text, which isn't very convenient IMO.
Simple middleware+decorator to handle age verification. Modeled after `django.contrib.sessions.middleware` to add an attribute to `request.user` called `is_age_verified` with consideration to [snippet 1002](http://www.djangosnippets.org/snippets/1002/). Decorator modeled after `django.contrib.auth.decorators.login_required`
Installation:
Create `verify_age` URLconf in `urls.py`
Create age verification page that URLconf points to
Define `settings.VERIFY_AGE_URL` based on URLconf
Add `age_verification.AgeVerificationMiddleware` to `MIDDLEWARE_CLASSES`
Import both `age_verification_required` and `REDIRECT_FIELD_NAME` in `views.py`
Implement `age_verification.AgeVerification.verify` somewhere to set session attribute on successful verification.
Use `@age_verification_required` decorator for views requiring age verification
Example urls.py:
urlpatterns += patterns('mahalo.answers.views',
...
url(r'^verify_age/?$', 'verify_age', name="verify_age"),
...
Example settings.py:
...
VERIFY_URL = '/verify_age/'
...
MIDDLEWARE_CLASSES += (
...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'myproject.contrib.middleware.age_verification.AgeVerificationMiddleware',
...
Example views.py:
from myproject.contrib.decorators import age_verification_required, REDIRECT_FIELD_NAME
from myproject.contrib.middleware.age_verification import AgeVerification
...
@age_verification_required
def some_view(request):
return render_to_response("index.html", {})
def verify_age(request):
# project specific
template_vars = default_template(request)
# form was posted
if request.POST.has_key("month") and request.POST.has_key("day") and \
request.POST.has_key("year"):
# "verify" user
av = AgeVerification(request.session)
av.verify()
if request.POST.has_key(REDIRECT_FIELD_NAME):
return HttpResponseRedirect(request.POST[REDIRECT_FIELD_NAME])
else:
return HttpResponseRedirect(reverse("root"))
# no form posted, show it
else:
if request.GET.has_key(REDIRECT_FIELD_NAME):
template_vars["next"] = request.GET[REDIRECT_FIELD_NAME]
return render_to_response("verify_age.html", template_vars)
These examples assume `age_verification.py` lives in `myproject/contrib/middleware/` and `decorators.py` lives in `myproject/contrib/`
This is a small approach to have a middleware which automatically creates a ticket in an existing Trac environment.
**Note:** you must have the [XML-RPC-Plugin](http://trac-hacks.org/wiki/XmlRpcPlugin) installed.
Extend the attrs-dict to your needs. For example: in my case I have the SensitiveTicket-Plugin installed - automatically created tickets are marked as sensitive and are not visible to the public.
Add RequestMiddleware to your MIDDLEWARE_CLASSES settings
Then, when you need request in special cases, call get_request(), which returns the request object.
This has to be used in very special cases.
Based *very heavily* on the middleware in [this snippet](http://www.djangosnippets.org/snippets/727/). As with that one, append '?prof' to the URL to see profiling output instead of page output. The big change is that you can also pass an argument to control sorting.
For example, you can append '?prof=cumulative' to sort the results by the cumulative time consumed. See the [documentation on the Stats class](http://docs.python.org/library/profile.html#pstats.Stats.sort_stats) for all the options.
This middleware implements a "soft timeout". This means the admin is sent an email whenever a page time exceeds a certian value. It is intended to run on production servers to inform the admin of any pages which are performing slowly.
Heavily based on [Snippet 1033](http://www.djangosnippets.org/snippets/1033/) and [Snippet 766](http://www.djangosnippets.org/snippets/766/).
This snippet tracks what view and templates are used to render HTML responses and inserts a small dialog in the top right corner of the output with links to all the files. If your text editor support opening files from a browser protocol you can click the links to open the files right up! For example TextMate supports the `txmt://` protocol. Really saves some time if you find yourself editing a lot of templates.

**Usage**
1. Save this snippet in a file called `middleware.py` on your Python Path.
2. Add `middleware.EditingMiddleware` to your `MIDDLEWARE_CLASSES`.
3. Browse to any HTML page on your site!
the snippet improve juliocarlos's greate works(see [http://www.djangosnippets.org/snippets/1235/](http://www.djangosnippets.org/snippets/1235/) ) ,merge functtions to one middlewere class, fixed url regular expression and eliminate AJAX support etc...
it's tested with django 1.0.2 and work fine on my wap site.
* the middlewere must before SessionMiddlewar in MIDDLEWARE_CLASSES tuple
eg:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'middleware.cookieless_session.CookielessSessionMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
Just a quick tip on how to use the pyfacebook django middleware as a decorator since you probably dont want to add the facebook object to all incoming requests.
Permit to redirect desired domain name to the 'domain' of Site app.
Useful if you have different domains name for the same website.
#1. Add to your settings DOMAINS_ALIAS like this:
DOMAINS_ALIAS = (
'my-second-domain.com',
'www.my-second-domain.com',
'third-domain.com',
'www.third-domain.com',
)
notice: all these domains are redirected to the **domain** db entry of Site ID.
#2. add all these domains to ServerAlias directive in your vhost apache configuration.
#3. enable the middleware by adding to your MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
...
'utils.middleware.domainsalias.DomainsAliasMiddleware',
...
)