你好我想问一下如何查看我在django admin上传的图片到选定的html页面,这是我的html,对象标题没有问题,但我似乎无法加载错误指示的图像它是
Not Found: /media/products/greenfood_MHL4ssO.jpg
[09/Nov/2017 15:25:19] "GET /media/products/greenfood_MHL4ssO.jpg HTTP/1.1"
404 3103
the image file is stored here
>>C:\trydjango\static\media\products\greenfood_MHL4ssO.jpg
the image url is
/media/products/greenfood_MHL4ssO.jpg
<h3>{{object.title}}</h3>
{% if object.productimage_set.count > 0 %}
<div>
{% for img in object.productimage_set.all %}
{{ img.image.file}}
<img class='img-responsive' src='{{ img.image.url }}'/>
{% endfor %}
</div>
def image_upload_to(instance, filename):
title = instance.product.title
slug = slugify(title)
file_extension = filename.split(".")[1]
new_filename = "%s.%s" %(instance.id, file_extension)
return "products/%s/%s" %(slug, new_filename)
class ProductImage(models.Model):
product = models.ForeignKey(Product)
image = models.ImageField(upload_to=image_upload_to)
def __unicode__(self):
return self.product.title
答案 0 :(得分:0)
settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py:
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
图片应存储在C:\trydjango\media\products\greenfood_MHL4ssO.jpg
中,媒体不应存放在静态文件夹中。