我只看到像/media/image.jpg这样的网址图标。我认为urls.py中存在问题,但我不知道如何解决它。有人能帮助我吗?
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'),
)
答案 0 :(得分:1)
我认为你必须在你的urls.py文件中添加它:
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
要好心,你只能将其用于开发者:Django doc
答案 1 :(得分:0)
/media/image.jpg
是相对网址。
要获得绝对的uri,你需要这样的request.build_absolute_uri(obj.image.url)
您可以阅读更多信息here in the django docs