python - Django Static image File Failed to load resource -
fixed solution, in main programs urls.py had add these lines.
from django.conf import settings django.conf.urls.static import static
urlpatterns += static(settings.media_url, document_root=settings.media_root)
i have model called item within there have image field. added new item image database , tried loading inside of index template. dont image when @ console says ("websitename/site_media/items/image.jpg 404 (not found)")
i think problem lies within settings.py file cant figure out did wrong here.
index.html template
{% load static %} <div class="item" style="background-image: url('{{ item.img.url }}')">`
model.py
class item(models.model): name = models.charfield(max_length=200) img = models.imagefield(upload_to='items', default='', blank=true, null=true) def __str__(self): return "%s" % (self.name)
views.py
def index(request): latestitems = item.objects.all().order_by('-id')[:3][::-1] return render(request, 'webshop/index.html', {'latestitems' :latestitems})
settings.py
site_root = os.path.dirname(os.path.realpath(__file__)) media_root = os.path.join(site_root, 'site_media/') media_url = 'site_media/'
in settings.py
base_dir = os.path.dirname(os.path.dirname(__file__)) staticfiles_dirs = ( os.path.join(base_dir, "static"), ) static_url = '/static/'
and read document
with directory root structure have shown, think above setting should work. have not tested though. let me know if works.
Comments
Post a Comment