我已经在谷歌和堆栈上读到了这个问题,但没有解决问题。 我想显示来自imagefield的图片。 设置
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
网址
urlpatterns += patterns('',
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
模型
class Item(models.Model):
name = models.CharField(max_length=255, verbose_name="Названия товара")
price = models.IntegerField(default=0, verbose_name="Цена")
image = models.ImageField(verbose_name="Фото", upload_to='item')
alias = models.SlugField(verbose_name="alias категория")
视图
def item(request, alias):
try:
tovar = Item.objects.get(alias=alias)
except Item.DoesNotExist:
raise Http404('Not Found ')
context = {
'tovar': tovar,
}
return HttpResponse(render_to_string("item.html", context))
模板
<div class="container">
{% for item in tovars %}
<div class="product-image-wrapper" style="float:left;height:320px;width:150px;margin-left:40px;">
<br/>
<div class="single-products">
<div class="productinfo text-center">
<img class="img-product" src="{{ item.image.url }}" alt=""/>
<h2 id="{{ item.id }}" class="value">{{ item.price }}</h2>
<p class="name">{{ item.name }}</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart ">
</i>Add to cart</a>
</div>
</div>
</div>
{% endfor %}
</div>
我在浏览器中打开此视图并且它不会加载图像。 http://127.0.0.1:777/media/item/quu3_Lj1KUoR.jpg 500(内部服务器错误)
Internal Server Error: /media/item/quu3_frHnLS9.jpg
Traceback (most recent call last):
File "E:\gited\shop\venv\lib\site-packages\django\core\handlers\base.py", line 235, in get_response
response = middleware_method(request, response)
File "E:\gited\shop\venv\lib\site-packages\django\middleware\clickjacking.py", line 31, in process_response
if response.get('X-Frame-Options') is not None:
AttributeError: 'Http404' object has no attribute 'get'
文件夹包含项目文件夹,该文件夹包含该图像。似乎链接到图像是真实的,但它拒绝加载它们。 如何解决这个问题?