我在python 2.7中使用django 1.5.4进行了尝试,我试图找出导致此图像显示为此类的原因 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
请帮助解决此问题是表单代码有问题还是? models.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)
答案 0 :(得分:0)
对于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}),