0
我有一個應用程序的django 1.6.2項目,它是一個存儲產品和postgresql數據庫的應用程序。儘管沒有發生圖像顯示,但所有配置都很順利。Django 1.6從數據庫中獲取文件的文件名
使用
> <a href="/{{ p.mainphoto }}"><img HEIGHT=175 WIDTH=175 src="{{
> p.mainphoto.name }}"></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))
和URL文件:
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)
utlimately我只是想「14333.jpg」,並在它之前添加網址,然後讓python自動添加圖像名稱w第i for循環讓我們知道你得到那個東西在引號
<a href="/webapps/static/images/1333.jpg"><img HEIGHT=175 WIDTH=175 src=""></a>
香港專業教育學院發現: '高清保存(個體經營,** kwargs): 進口OS 超(照片,個體經營).save(* * kwargs) new_name ='/'.join(self.file.file.name.split('/')[0:-1] + ['%s.jpg'%self.id]) os。 (self.file.file.name,new_name) self.file ='/'. * kwargs) photo_saved.send(sender = self)' – DInfinityD