我试图用reverse构建一个绝对url,但我得到了上面的错误。
代码:
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,
)
我得到错误
nameError: name 'request is not defined
如何导入请求?
我需要完整url的原因是,如果只使用反向,当我在本地运行应用程序时,我会收到以下错误,我不想硬编码120.0.0.1:8000/到url。
requests.exceptions.MissingSchema: Invalid URL '/api/v2/app/startconversations': No schema supplied. Perhaps you meant http:///api/v2/app/startconversations?
build_absolute_uri应该由HttpRequest实例调用。应该是这样的:
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)