mirror of https://github.com/django/django.git
[3.0.x] Refs #26601 -- Used new-style middlewares in documentation.
Backport of d71497bb24
from master
This commit is contained in:
parent
6b7bd079a6
commit
1319124aa9
|
@ -288,16 +288,17 @@ Methods
|
||||||
behind multiple proxies. One solution is to use middleware to rewrite
|
behind multiple proxies. One solution is to use middleware to rewrite
|
||||||
the proxy headers, as in the following example::
|
the proxy headers, as in the following example::
|
||||||
|
|
||||||
from django.utils.deprecation import MiddlewareMixin
|
class MultipleProxyMiddleware:
|
||||||
|
|
||||||
class MultipleProxyMiddleware(MiddlewareMixin):
|
|
||||||
FORWARDED_FOR_FIELDS = [
|
FORWARDED_FOR_FIELDS = [
|
||||||
'HTTP_X_FORWARDED_FOR',
|
'HTTP_X_FORWARDED_FOR',
|
||||||
'HTTP_X_FORWARDED_HOST',
|
'HTTP_X_FORWARDED_HOST',
|
||||||
'HTTP_X_FORWARDED_SERVER',
|
'HTTP_X_FORWARDED_SERVER',
|
||||||
]
|
]
|
||||||
|
|
||||||
def process_request(self, request):
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
"""
|
"""
|
||||||
Rewrites the proxy headers so that only the most
|
Rewrites the proxy headers so that only the most
|
||||||
recent proxy is used.
|
recent proxy is used.
|
||||||
|
@ -307,6 +308,7 @@ Methods
|
||||||
if ',' in request.META[field]:
|
if ',' in request.META[field]:
|
||||||
parts = request.META[field].split(',')
|
parts = request.META[field].split(',')
|
||||||
request.META[field] = parts[-1].strip()
|
request.META[field] = parts[-1].strip()
|
||||||
|
return self.get_response(request)
|
||||||
|
|
||||||
This middleware should be positioned before any other middleware that
|
This middleware should be positioned before any other middleware that
|
||||||
relies on the value of :meth:`~HttpRequest.get_host()` -- for instance,
|
relies on the value of :meth:`~HttpRequest.get_host()` -- for instance,
|
||||||
|
|
|
@ -169,15 +169,18 @@ Add the following middleware to :setting:`MIDDLEWARE`::
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.deprecation import MiddlewareMixin
|
|
||||||
|
|
||||||
class TimezoneMiddleware(MiddlewareMixin):
|
class TimezoneMiddleware:
|
||||||
def process_request(self, request):
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
tzname = request.session.get('django_timezone')
|
tzname = request.session.get('django_timezone')
|
||||||
if tzname:
|
if tzname:
|
||||||
timezone.activate(pytz.timezone(tzname))
|
timezone.activate(pytz.timezone(tzname))
|
||||||
else:
|
else:
|
||||||
timezone.deactivate()
|
timezone.deactivate()
|
||||||
|
return self.get_response(request)
|
||||||
|
|
||||||
Create a view that can set the current timezone::
|
Create a view that can set the current timezone::
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue