我有一个带有应用程序的django 1.6.2项目,该应用程序是一个带有产品和postgresql数据库的商店。一切都配置好了,虽然没有发现要显示的图像。
使用
> <a href="/{{ p.mainphoto }}"><img HEIGHT=175 WIDTH=175 src="{{
> p.mainphoto.name }}"></a>
结果来源显示:
<a href="/webapps/static/images/14333.jpg"><img HEIGHT=175 WIDTH=175 src="/webapps/static/images/14333.jpg"></a>
显然无法显示图像。
这是我的模型的样子
class ProductImage(models.Model):
property = models.ForeignKey('Product', related_name='images')
image = models.ImageField(upload_to='/static/images', blank=True)
def filename(self):
return os.path.basename(self.image.name)
class Product(models.Model):
name = models.CharField(max_length=300)
designers = models.ManyToManyField(Designer, blank=True, verbose_name = 'designer/artist')
width = models.DecimalField(blank=True, max_digits = 8, decimal_places=1, null=True)
height = models.DecimalField(blank=True, max_digits = 8, decimal_places=1, null=True)
depth = models.DecimalField(blank=True, max_digits = 8, decimal_places=1, null=True)
diameter = models.DecimalField(blank=True, max_digits = 8, decimal_places=1, null=True)
description = models.TextField()
colors = models.ManyToManyField(Color, blank=True)
materials = models.ManyToManyField(Material, blank=True)
mainphoto = models.ImageField(upload_to='/static/images', blank=True)
morephotos = models.ManyToManyField(ProductImage, blank=True)
manufacturer = models.CharField(max_length=300, blank=True)
price_in_dollars = models.DecimalField(max_digits=6, decimal_places=2)
type = models.ManyToManyField(FurnitureType, verbose_name = 'furniture/art type')
def __unicode__ (self):
return self.name
def filename(self):
return os.path.basename(self.mainphoto.name)
这是什么视图:
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import RequestContext
from django.views.generic.list import ListView
from django.utils import timezone
from store.models import Product
def home(request):
return render_to_response('homepage.html')
def designhomepage(request):
return render_to_response('designserviceshome.html')
def eventspage(request):
return render_to_response('events.html')
def aboutpage(request):
return render_to_response('about.html')
def returnpage(request):
return render_to_response('returnpolicy.html')
def privacypage(request):
return render_to_response('privacypolicy.html')
def hello(request):
name = ""
html = "<html><body>Hi %s. </body></html>" % name
return HttpResponse(html)
def hellonew(request):
name = ""
t = get_template('hello.html')
html = t.render(Context({'name': name}))
return HttpResponse(html)
def inventory(request):
products = Product.objects.all()
productinfo = {
"product_detail": products
}
return render_to_response('inventory.html', productinfo, context_instance=RequestContext(request))
和网址文件:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from store.models import Product
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
... urls
)
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我只想从下面的代码中获取“14333.jpg”并在它之前添加网站网址,然后让python自动添加带有for循环的图像名称让我们知道你获取该东西的想法在引号
<a href="/webapps/static/images/1333.jpg"><img HEIGHT=175 WIDTH=175 src=""></a>
答案 0 :(得分:0)
我通过以下方式解决了这个问题:
class ProductImages(models.Model):
product = models.ForeignKey('Product3', related_name='images')
image = models.ImageField(upload_to='/webapps/static', blank=True)
def __unicode__(self):
return os.path.basename(self.image.name)
def filename(self):
return os.path.basename(self.image.name)
class Product3(models.Model):
category = models.CharField(max_length=300)
slug = models.SlugField(max_length=150, blank=True)
name = models.CharField(max_length=300)
designers = models.CharField(max_length=300, blank=True)
manufacturer = models.CharField(max_length=300, blank=True)
description = models.TextField()
quantity = models.DecimalField(max_digits=6, decimal_places=2, blank=True)
price_in_dollars = models.DecimalField(max_digits=6, decimal_places=2)
width = models.DecimalField(max_digits=6, decimal_places=2, blank=True)
depth = models.DecimalField(max_digits=6, decimal_places=2, blank=True)
diameter = models.DecimalField(max_digits=6, decimal_places=2, blank=True)
height = models.DecimalField(max_digits=6, decimal_places=2, blank=True)
photo = models.ImageField(upload_to='/webapps/static', blank=True)
materials = models.CharField(max_length=300, blank=True)
style = models.CharField(max_length=300, blank=True)
timeperiods = models.CharField(max_length=300, blank=True)
country = models.CharField(max_length=300, blank=True)
additionalphotos = models.ManyToManyField(ProductImages, blank=True)
def __unicode__(self):
return self.name
def filenamemain(self):
return os.path.basename(self.photo.name)
这个模型看起来像这个重要的部分是def文件名(自我)部分
接下来是在模板中添加正确的调用,如下所示:(注意{{p.filename}})
{%block body_content %}
<div class="">
<div id="manyphotos">
{% for p in itemdetail %}
<a href="http://www.yoursite.com/static/{{p.filename}}"><img HEIGHT=175 WIDTH=175 src="http://www.yoursite.com/static/{{p.filename}}" border="0"></a>
{% endfor %}
</div>
</div>
{%endblock%}
使用正确的主键正确浏览整个数据库并显示所有可用图像。
此视图如下所示:
def itemdetailpage(request, id):
property = Product3.objects.get(pk=id)
property1 = property.images.all()
itemin = {"itemdetail": property1 }
return render_to_response('details.html', itemin, context_instance=RequestContext(request))