gpt4 book ai didi

python - 我无法在 Django 模板上显示图像

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:55 27 4
gpt4 key购买 nike

我正在使用 django 1.5.4 和 python 2.7 我试图弄清楚这张图片显示这样的原因是什么view.py

def single(request,slug):
product = Product.objects.get(slug=slug)
images = product.productimage_set.all()
categories = product.category_set.all()

context = {
"product":product,
"categories":categories,
"edit":True,
"images":images,
}
return render_to_response("products/single.html", context, context_instance=RequestContext(request))

和single.html

{% extends "base.html" %}
{% block content %}
<h1>{{ product }}</h1>
{% for abc in images %}
<img src="{{ MEDIA_URL }}{{ abc.image }}" />
{% endfor %}
<ul>
{% for category in categories %}
<li>{{ category }}</li>
{% endfor %}
</ul>

{% if edit %}
<a href="#">Edit this product</a>
{% endif %}
{% endblock %}

但是它显示的是这个 picture

请帮忙解决这个问题是表单代码的问题还是?模型.py

class Product(models.Model):
user = models.ForeignKey(User,null=True,blank=True)
title = models.CharField(max_length=180)
description = models.CharField(max_length=500)
price = models.DecimalField(max_digits=20,decimal_places=2)
sale_price = models.DecimalField(max_digits=20,decimal_places=2,null=True,blank=True)
slug = models.SlugField()
order = models.IntegerField(default=0)
timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)
updated = models.DateTimeField(auto_now_add=False,auto_now=True)
active = models.BooleanField(default=True)


def __unicode__(self):
return str(self.title)

class Meta:
ordering = ['-order']

class ProductImage(models.Model):
product = models.ForeignKey(Product)
image = models.ImageField(upload_to="products/image/")
title = models.CharField(max_length=120,null=True,blank=True)
featured_image = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)
updated = models.DateTimeField(auto_now_add=False,auto_now=True)


def __unicode__(self):
return str(self.title)

最佳答案

对于 django 1.5.4 你必须 setting.py 和编辑 MEDIA_ROOT

MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),"static","media")

MEDIA_URL = '/media/' ###This depends on the location of your media files### As this was for the location of my files

urls.py

url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),

关于python - 我无法在 Django 模板上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com