Reputation: 131
My images are not loading in a template. I was placing them in admin, so everything should be ok but,no. Model field
class Guide(models.Model):
image = models.ImageField(upload_to='users/%Y/%m/%d/', blank=True)
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = "/static/"
MEDIA_URL = '/trips/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'trips/media/')
Project tree
Template
{% if item.image%}
<img src="{{item.image.url}}" alt="" style="max-height:300px">
{%else%}
<img src="{%static 'avatar_sample.png' %}" id="uploaded_image" class="img-responsive img-circle" />
{%endif%}
Upvotes: 0
Views: 299
Reputation: 36
make sure you import these settings in urls.py
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Upvotes: 1