我有一个帖子请求
http://localhost:8000/api/orders/shipment
我想要做的是我不想传递域/IP地址并访问api之类的Django管理控制台给出“172.18.0.1 - - [08/Sep/2017 14:30:29]”POST /adminorder/order/导入/HTTP/1.1“200”
我在 django 文档中搜索了 get_absolute_url() 方法来定义自定义 url,我不知道如何在这种情况下使用它。
我有一个帖子请求
http://localhost:8000/api/orders/shipment
我想要做的是我不想传递域/IP地址并访问api之类的Django管理控制台给出“172.18.0.1 - - [08/Sep/2017 14:30:29]”POST /adminorder/order/导入/HTTP/1.1“200”
我在 django 文档中搜索了 get_absolute_url() 方法来定义自定义 url,我不知道如何在这种情况下使用它。
我相信在请求中使用相对 url 是不可能的,但是您可以通过创建一个将相关域添加到您的 url 的函数来解决这个问题。(确保导入您的设置文件!)
from django.conf import settings
development_url = "http://localhost:8000/"
production_url = "http://myapisite.com/"
def create_url(relative_url):
# If you are on your development server. This is assuming your development always has DEBUG = True, and your production is always DEBUG = False
if settings.DEBUG:
return development_url + relative_url
# Production
return production_url + relative_url
因此print(create_url("api/test/anothertest"))
会返回http://localhost:8000/api/test/anothertest
。
这就是你将如何使用它:
url = create_url("api/orders/shipment/")
data = requests.post(url=url, data=content, headers = headers)