{{{
from django.test import RequestFactory, AsyncRequestFactory
factory = RequestFactory()
afactory = AsyncRequestFactory()
request = factory.get("/customer/details", REMOTE_ADDR="127.0.1.1")
assert request.META["REMOTE_ADDR"] == "127.0.1.1"
request = afactory.get("/customer/details", REMOTE_ADDR="127.0.1.1")
# will fail
assert request.META["REMOTE_ADDR"] == "127.0.1.1"
}}}
I think there are some other variables also affected. I tested it only
with django 5.0.
--
Ticket URL: <https://code.djangoproject.com/ticket/35082>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by alex):
Workaround: often it is enough to use RequestFactory even it is incorrect
--
Ticket URL: <https://code.djangoproject.com/ticket/35082#comment:1>
* status: new => closed
* resolution: => invalid
Comment:
According to the [https://asgi.readthedocs.io/en/latest/specs/main.html
ASGI spec], `REMOTE_ADDR` is based on the `client` in the connection
scope, so you can override it with:
{{{#!python
>>> afactory = AsyncRequestFactory(client=("127.0.1.1", "88"))
>>> request = afactory.get("/somewhere")
>>> request.META["REMOTE_ADDR"]
"127.0.1.1"
}}}
If you're having trouble understanding how Django works, see
TicketClosingReasons/UseSupportChannels for ways to get help.
--
Ticket URL: <https://code.djangoproject.com/ticket/35082#comment:2>