Reputation: 287
I have some model with a FileField() looks like this:
image = models.FileField(upload_to='/Users/john/projects/MyDjangoApp/foobars/images/')
So foobar is some module from MyDjangoApp. In future there comes another module like barfoos and this will have an own images folder.
What I don't understand is how to handle this in the template. Now there stand something like:
<td><img src="{{ foobar.image.url }}" height="350" width="350"></td>
So there stand the following:
/Users/john/projects/MyDjangoApp/foobars/images/71RkbKwBxnL._SL1004_.jpg
But it must be, something like:
http://127.0.0.1:8000/foobars/images/71RkbKwBxnL._SL1004_.jpg
All url patterns I find only handle this if the images hosted in /MyDjangoApp/static/images, I wish to handle the files in /MyDjangoApp/foobars/images and later /MyDjangoApp/barfoos/images.
Ho to handle this in Django.
Thanks a lot for your time!
Upvotes: 0
Views: 236
Reputation: 53679
The upload_to
argument should be a path relative to your MEDIA_ROOT
setting. The url that you'll use to access the file is the same relative path appended to your MEDIA_URL
setting.
Also check out the documentation on FileField
s and ImageField
s and on the MEDIA_ROOT
and MEDIA_URL
settings
Upvotes: 2