Passing Django request object into script to build absolute URI?

I am trying to build a one time script by passing in a django request in order to build an absolute URI.

I am using the HttpRequest() method as follows...

request = HttpRequest()

And passing it into the function that I am calling when running my script.

I keep running into a KeyError

My script that is being ran is as follows,

from django.http import HttpRequest

for form in forms:

    form_data = get_data(form)

    for user in User.objects.all():

        request = HttpRequest()

        config_user_forms(request, user)

Errors out with the following,

  File "config_user_forms.py", line 65, in <module>
    redeploy_form(request, user)
  File "/tmp/8dae22604556cc2/users/admin/users.py", line 654, in redeploy_form
    build_url = request.build_absolute_uri(
  File "/tmp/8dae22604556cc2/antenv/lib/python3.8/site-packages/django/http/request.py", line 223, in build_url
    location = self._current_scheme_host + location
  File "/tmp/8dae22604556cc2/antenv/lib/python3.8/site-packages/django/utils/functional.py", line 45, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/tmp/8dae22604556cc2/antenv/lib/python3.8/site-packages/django/http/request.py", line 240, in _current_scheme_host
    return "{}://{}".format(self.scheme, self.get_host())
  File "/tmp/8dae22604556cc2/antenv/lib/python3.8/site-packages/django/http/request.py", line 121, in get_host
    host = self._get_raw_host()
  File "/tmp/8dae22604556cc2/antenv/lib/python3.8/site-packages/django/http/request.py", line 111, in _get_raw_host
    host = self.META["SERVER_NAME"]
KeyError: 'SERVER_NAME'

I am a bit stumped on a work around, and appreciate any help in advance!

Back to Top