'nameError: name 'request is not defined
I'm trying to build an absolute url with reverse but I get the above error.
Code:
def get_endpoint(payload):
url = request.build_absolute_uri(reverse("app-start-conversation"))
data = json.dumps(payload)
response = requests.post(url, data, headers=head)
Urls.py:
path(
"api/v2/app/startconversations",
views.StartConversation.as_view(),
name="app-start-conversation,
)
I get the error
nameError: name 'request is not defined
How do I import request?
The reason I need the full url is because with reverse alone, when I run the app locally I get the following error and I do not want to hardcode 120.0.0.1:8000/ to the url.
requests.exceptions.MissingSchema: Invalid URL '/api/v2/app/startconversations': No schema supplied. Perhaps you meant http:///api/v2/app/startconversations?
Solution 1:[1]
build_absolute_uri should be called by an HttpRequest instance. Should be something like:
from django.http import HttpRequest
def get_endpoint(payload):
request = HttpRequest()
url = request.build_absolute_uri(reverse("app-start-conversation"))
data = json.dumps(payload)
response = requests.post(url, data, headers=head)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ankitbatra22 |
