Reputation: 47
I see only icons with urls like /media/image.jpg. I think problem in urls.py, but I don't know how to resolve it. Can someone help me?
settings.py:
...
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
....
models.py:
....
class Image(models.Model):
product = models.OneToOneField(Product)
top_image = models.ImageField(upload_to='media/', verbose_name ="Главное изображение")
second_image = models.ImageField(blank = True, verbose_name = "Изображение 2")
third_image = models.ImageField(blank = True, verbose_name = "Изображение 3")
fourth_image = models.ImageField(blank = True, verbose_name = "Изображение 4")
fifth_image = models.ImageField(blank = True, verbose_name = "Изображение 5")
def __unicode__(self):
return u'%s' % self.product
class Meta():
db_table = "Image"
...
articles.html
...
{% for image in product_images %}
<div>
<img src="{{ image.top_image.url }}">
</div>
{% endfor %}
...
urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mypoject.views.home', name='home'),
url(r'^', 'article.views.articles'),
)
Upvotes: 2
Views: 263
Reputation: 694
I think you have to add this in your urls.py file :
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Be carreful you have to use it only for dev : Django doc
Upvotes: 1
Reputation: 2022
/media/image.jpg
is a relative url.
To get the absolute uri you need something like this request.build_absolute_uri(obj.image.url)
You can read further informations here in the django docs
Upvotes: 0