mostrar - media url django
MEDIA_ROOT Django no encuentra imágenes (1)
<img src =''{{ "MEDIA_URL" }}{{ image }}''/>
Está Mal. Necesitas
<img src =''{{ MEDIA_URL }}{{ image }}''/>
o
<img src =''{{ image.url }}''/>
Dado que ImageField
hereda todos los métodos y atributos de FileField
, tiene acceso a la propiedad .url
llamada .url
que llama al método url()
de la clase Storage
. Devolverá la ruta relativa que incluye /media/
folder.
Quiero mostrar imágenes * .png en mi plantilla pero no puedo! Todas las demás cosas de la página se visualizan bien (arranque en ruta http, css desde archivos locales). Pierdo algunas horas tratando de establecer el camino, pero sin efecto. Parece que intenté todo lo que pude inventar. Por favor ayuda.
por favor vea el árbol del proyecto
settings.py
...
import os
''''''path settings for django==1.8''''''
# BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
TEMPLATE_DIRS = (
#''/Users/jmitch/Desktop/seven/static/templates/'',
os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), "static", "templates"),
)
TEMPLATES = [
{
''BACKEND'': ''django.template.backends.django.DjangoTemplates'',
# ''DIRS'': [BASE_DIR+"/templates", ],
''DIRS'': [BASE_DIR + "/static/templates/", ],
''APP_DIRS'': True,
''OPTIONS'': {
''context_processors'': [
''django.template.context_processors.debug'',
''django.template.context_processors.request'',
''django.contrib.auth.context_processors.auth'',
''django.contrib.messages.context_processors.messages'',
''django.core.context_processors.media'',
],
},
},
]
ENV_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
MEDIA_ROOT = os.path.join(ENV_PATH, ''media'')
# MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))), ''static'', ''media'')
# MEDIA_ROOT = ''/Volumes/Storage/_codework/e_shop/static/media/product/images/''
# MEDIA_ROOT = os.path.join(BASE_DIR,"media/images")
MEDIA_URL = ''/media/''
STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), ''static'', ''static-only'')
STATIC_URL = ''/static/''
STATICFILES_DIRS = (
os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), ''static'', ''static''),
)
...
all.html
.....
{% block content %}
{% for product in products %}
<div class=''container-fluid''>
<div class=''col-md-6''>{{ product.title }}
{{ product.price }}
{% for image in product.productimage_set.all %}
<img src =''{{ "MEDIA_URL" }}{{ image }}''/>
</div>
{% endfor %}
</div>
{% endfor %}
{{ image }}
{% endblock %}
.......
ecommerce / urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('''',
(r''^static/(?P<path>.*)$'', ''django.views.static.serve'', {
''document_root'': settings.STATIC_ROOT
}),
(r''^media/(?P<path>.*)$'', ''django.views.static.serve'', {
''document_root'': settings.MEDIA_ROOT
}),
url(r''^admin/doc/'', include(''django.contrib.admindocs.urls'')),
url(r''^admin/'', include(admin.site.urls)),
url(r''^products/'', include(''products.urls'')),
url(r''^contact/'', ''contact.views.contact_us'', name=''contact_us''),
)
productos / urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
urlpatterns = patterns(''products.views'',
url(r''$'', ''all_products'', name=''products''),
)
models.py
from django.db import models
class Product(models.Model):
title = models.CharField(max_length=220)
description = models.CharField(max_length=3000, null=True, blank=True)
price = models.DecimalField(max_digits=1000, decimal_places=2, null=True, blank=True)
slug = models.SlugField()
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.title
'''''' sort item alphabetically ''''''
class Meta:
ordering = [''title'']
class ProductImage(models.Model):
product = models.ForeignKey(Product)
description = models.CharField(max_length=3000, null=True, blank=True)
image = models.ImageField(upload_to=''/product/images'')
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.image