5
The upload_to=…
parameter [Django-doc] is relative to the MEDIA_ROOT
. So if you want to store it in a directory uploads
in the media
directory, you upload this with:
class Product(models.Model):
# …
thumbnail = models.ImageField(upload_to='uploads/', blank=True, null=True)
In order to render the URL however, you use the .url
attribute [Django-doc], so:
{% if p.thumbnail %}
<img src="{{ p.thumbnail.url }}">
{% endif %}
The {% if p.thumbnail %}
is here necessary to check for NULL
/None
values.
2
it’s cause you wrote "upload_to=’media/uploads’"..
from your settings django will create media folder and since you wrote "media/uploads" ,uploads is inside the media folder which is inside main media which was declared in settings. So just write
class Product(models.Model):
…
thumbnail = models.ImageField(upload_to='uploads/', blank=True, null=True)
- [Django]-How to Show and hide form fields based on a selection from a dropdown menu in Django
- [Django]-Django: How to restart webserver so that changes in sourcecode get applied
- [Django]-Django WSGI deployment. cannot import name 'SimpleCookie'
Source:stackexchange.com