【问题标题】:Display full Image URL on JSON output using Django Tastypie使用 Django Tastypie 在 JSON 输出中显示完整的图像 URL
【发布时间】:2015-07-15 05:58:55
【问题描述】:

我创建了一个 json API,可以在HERE找到它

我遇到的问题是图像 URL 没有完全显示。 例如当前的输出是:

图片:“/media/images/ShanRoberts_NWales_Vitocal300A_xgGCGrG.JPG”,

我需要它显示为:

图片:“http://178.62.67.237/media/images/ShanRoberts_NWales_Vitocal300A_xgGCGrG.JPG”,

有什么想法吗?

【问题讨论】:

标签: json django django-models tastypie


【解决方案1】:

您可以将from django.contrib.staticfiles.templatetags.staticfiles import staticHttpRequest.build_absolute_uri 组合起来获取完整的网址。

from django.templatetags.static import static
from django.http.HttpRequest import build_absolute_uri

relative_url = static('x.jpg')
# url now contains '/static/x.jpg', assuming a static path of '/static/'
absolute_url = build_absolute_uri(relative_url)

【讨论】:

  • 我是否将其添加到我的 models.py 中?抱歉,我从未处理过 URL!
  • 我猜你可以将它添加到你的模型中,但最好不要将它保存在你的数据库中,以防你更改静态文件夹或 DNS 或 ip!你可以只使用 build_absolute_uri 在你的情况下!
  • 那被称为“请求”的导入呢?我必须 pip install 才能使用它吗?
  • 我的错,是HttpRequest
  • 我收到一个导入错误--> ImportError: No module named HttpRequest
猜你喜欢
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 2021-12-07
  • 2016-04-21
  • 2018-08-05
相关资源
最近更新 更多