Reputation: 993
Exactly as the title says i cannot load the image i saved in the database.
Im am saving the uploaded images in "TV/media/logo_image/" Thi is my MEDIA_URL:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT= os.path.join(BASE_DIR, 'Tv/media')
My model:
class EmpresaProfile(models.Model):
empresa = models.CharField(max_length=20, blank=False, default="")
logo = models.ImageField(upload_to='logo_image', blank=True)
def __str__(self):
return self.empresa
This is my views.py:
def index(request):
empresa = EmpresaProfile.objects.all()
return render_to_response('Tv/index.html',{'empresa': empresa})
template:
<div style="height: 100px;">
{% for empresa.logo in empresa %}
<img class="img-responsive" src="{{MEDIA_URL}}{{ empresa.logo }}" alt="" width="1000px" />
{% endfor %}
</div>
This is the vey first page i access so this is mu url:
urlpatterns = [
url(r'^$', views.index, name='index'),]
I am getting source "unknown" in the image tag
thank you
Upvotes: 1
Views: 955
Reputation: 1130
The correct way to do it for static files is by using
static()
For media, you should do something like this:
<img src="{{ product_image }}" alt="" />
And in the view function for that page:
link_to_home_page = 'http://127.0.0.1:8000'
product_image = link_to_home_page + one_new_product.main_picture.url
Also don't forget for development server, use this:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT + os.path.altsep)
Upvotes: 0
Reputation: 14391
You should change your forloop in template like this
<div style="height: 100px;">
{% for emp in empresa %}
<img class="img-responsive" src="{{MEDIA_URL}}{{ emp.logo }}" alt="" width="1000px" />
{% endfor %}
</div>
Upvotes: 1