Skip to content

Commit

Permalink
[3.0.x] Refs #26601 -- Used new-style middlewares in documentation.
Browse files Browse the repository at this point in the history
Backport of d71497b from master
  • Loading branch information
claudep authored and felixxm committed Sep 24, 2019
1 parent 6b7bd07 commit 1319124
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 6 additions & 4 deletions docs/ref/request-response.txt
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,17 @@ Methods
behind multiple proxies. One solution is to use middleware to rewrite
the proxy headers, as in the following example::

from django.utils.deprecation import MiddlewareMixin

class MultipleProxyMiddleware(MiddlewareMixin):
class MultipleProxyMiddleware:
FORWARDED_FOR_FIELDS = [
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED_HOST',
'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
recent proxy is used.
Expand All @@ -307,6 +308,7 @@ Methods
if ',' in request.META[field]:
parts = request.META[field].split(',')
request.META[field] = parts[-1].strip()
return self.get_response(request)

This middleware should be positioned before any other middleware that
relies on the value of :meth:`~HttpRequest.get_host()` -- for instance,
Expand Down
9 changes: 6 additions & 3 deletions docs/topics/i18n/timezones.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,18 @@ Add the following middleware to :setting:`MIDDLEWARE`::
import pytz

from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin

class TimezoneMiddleware(MiddlewareMixin):
def process_request(self, request):
class TimezoneMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
tzname = request.session.get('django_timezone')
if tzname:
timezone.activate(pytz.timezone(tzname))
else:
timezone.deactivate()
return self.get_response(request)

Create a view that can set the current timezone::

Expand Down

0 comments on commit 1319124

Please sign in to comment.