I try to build my first application with Django, and use django-storage to store my media on dropbox:
DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
DROPBOX_OAUTH2_TOKEN = ---- mytoken ----
DROPBOX_ROOT_PATH = 'media'
It works well to upload and display the images using the models directly (e.g. {{ object.photo.url }}
in the html template works)
However, how can I create a link to the image in the template if I have no object.
Something like
<img src="{{media_url}}myimage.jpg">
does not work as it links to localhost (on my local server). Is it needed to redefine media_url or is there another tag to use?
I am not sure about the existence of any tag to use in the case of accessing the image from dropbox. I would solve this issue using custom filters
in Django since it is the case of distinguishing between local
and remote
and forming the right URL
to pass to the src
attribute.
You can read from the docs here on how custom filter
works. Essentially, you would need a custom function that prepends the remote/local URL
.
Using a function like this for e.g.
@register.filter
@stringfilter
def prepend(str):
url = "" #defien your remote url here
return "{}{}".format(url, str)
You then make calls from the template to this function which would give you the correct url (as you defined it).