0

I have created a json API which can be found HERE

The problem I have is that the Image URL is not displayed fully. For example the currect output is :

image: "/media/images/ShanRoberts_NWales_Vitocal300A_xgGCGrG.JPG",

I need it to be displayed like :

image: "http://178.62.67.237/media/images/ShanRoberts_NWales_Vitocal300A_xgGCGrG.JPG",

Any ideas?

AndroidKrayze
  • 349
  • 2
  • 5
  • 21

1 Answers1

0

You can combine from django.contrib.staticfiles.templatetags.staticfiles import static and HttpRequest.build_absolute_uri to get the full URL.

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)
e-nouri
  • 2,576
  • 1
  • 21
  • 36
  • Do I add this to my models.py? Sorry but I have never worked on URLS! – AndroidKrayze May 05 '15 at 14:29
  • I guess that you can add this to your model, but better not to save it in your database in case you change static folder or your DNS or ip ! You can use just build_absolute_uri in your case! – e-nouri May 05 '15 at 14:34
  • What about the import which is called 'request'? Wat do I have to pip install in order to make use of it? – AndroidKrayze May 05 '15 at 14:42
  • my bad, it is HttpRequest – e-nouri May 05 '15 at 14:42
  • I am getting an Import Error--> ImportError: No module named HttpRequest – AndroidKrayze May 05 '15 at 14:44
  • This document explains the APIs for HttpRequest and HttpResponse objects, which are defined in the django.http module. here is the doc mate: https://docs.djangoproject.com/en/1.8/ref/request-response/#module-django.http – e-nouri May 05 '15 at 14:50