2014-02-19 51 views
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> 
+0

香港專業教育學院發現: '高清保存(個體經營,** 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

回答

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))