I want to change the default IP address obtained by the django app running on Heroku from REMOTE_ADDR to HTTP_X_FORWARDED_FOR.
So I created middleware.py in the same hierarchy as settings.py and made the contents as follows.
from django.http import HttpResponse class RemoteAddrMiddleware (object): def __init __ (self, get_response): self.get_response = get_response def __call __ (self, request): return self.get_response (request) def process_request (self, request): if'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META ['HTTP_X_FORWARDED_FOR'] .split (',')  .strip () request.META ['REMOTE_ADDR'] = ip
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', .. .. 'myproject.middleware.RemoteAddrMiddleware', # Addendum ]
However, it was not successful, and I would be grateful if anyone could give me some advice on what to do.
Answer # 1
MIDDLEWARE_CLASSESIt is an implementation that seems to be confused with.
(Check the version to see if it was implemented based on old information)
First, use logging etc.
process_requestLet's check if is called.
__call__Does not have a process_request call.
In conventional middleware, process_request is called directly, so
I think that the "implementation method" and "configuration method" of the middleware are based on the information of different versions.
MIDDLEEAREWhen setting to
Middleware classes inherit from MiddlewareMixin
__call__Do not override
django document --middleware
Upgrade Django 1.10 or earlier style middleware
About middleware for reverse proxy
This is old information, but please check the following information as well.
HTTP_X_FORWARDED_FOR references must be trusted
It is written that it can be disguised.
1.0 had django.middleware.http.SetRemoteAddrFromForwardedFor, so why not check out alternative classes and more?
Removed SetRemoteAddrFromForwardedFor middleware
The implementation before it was removed is as follows
MIDDLEWARE_CLASSESIt is used in.
class SetRemoteAddrFromForwardedFor (object): "" " Middleware that sets REMOTE_ADDR based on HTTP_X_FORWARDED_FOR, if the latter is set. This is useful if you're sitting behind a reverse proxy that causes each request's REMOTE_ADDR to be set to 127.0.0.1. Note that this does NOT validate HTTP_X_FORWARDED_FOR. If you're not behind a reverse proxy that sets HTTP_X_FORWARDED_FOR automatically, do not use this middleware. Anybody can spoof the value of HTTP_X_FORWARDED_FOR, and because this sets REMOTE_ADDR based on HTTP_X_FORWARDED_FOR, that means anybody can "fake" their IP address. Only use this when you can absolutely trust the value of HTTP_X_FORWARDED_FOR. "" " def process_request (self, request): try: try: real_ip = request.META ['HTTP_X_FORWARDED_FOR'] except KeyError: return None else: else: # HTTP_X_FORWARDED_FOR can be a comma-separated list of IPs. The # client's IP will be the first one. real_ip = real_ip.split (",")  .strip () request.META ['REMOTE_ADDR'] = real_ip
- i want to change the default version of python
- i want to change the default of python to python3 series
- python 3x - change font size for python3 tkinter entry
- python - i want to use asyncio with django custom commands
- i want to change the default settings of a control in visual studio
- python - django image folder reasons to create static and madia folders separately
- python 3x - change row name/column name of csv file
- python - does pixel information change when cv2imwrite processing is performed?
- [python] [tkinter] i want to change the acquisition contents of the second and subsequent menu lists by selecting the previous p
- python - about image deletion in django
- python - object is not displayed in html in django queryset
- i want to dynamically change the value of a variable in python and execute it
- python 3x - i want to change the background color of the specified line in python3 tkinter treeview
- name error in underscore in python django
- python - how to count url clicks in django and record them in the db
- python - how to change the page to redirect when submitting depending on the value selected in the pull-down in the form
- php - [wordpress] i want to change the default to "upload to this post" in the media selection refinement dropdown
- python - django i want to filter the data associated with a foreign key and display it in a template
- python - [django] gte works with filter in queryset, but lt doesn't work and i can't get the specified range of data
- python - django's genericlistview override get_queryset is not reflected
- i want to solve the problem that the strikethrough line "~~ strikethrough ~~" and the checkbox "- task 1"
- i want to solve the problem that the quote ">" of markdown technique cannot be converted to html in python-markdown
- python - about django's listview
- python - posted by mistake
- unable to get database values in python django
- python - i can't transition the screen of django
- python - i deployed a django project on ec2 but the static files don't load