python - Django serving images from database -
i adding trying app serve image fields in database display in template. wondering if there way bypass having use global media dir/url setting, sake of organization. in modal have set , upload option. id use url mapping app regex named groups tell view path , image name is. concatenate string , serve file. if terrible idea, or grossly inefficient, i'll else. appreciated. here have far.
template:`
{% category in items %} <div class="col-sm-4 col-lg-4 col-md-4"> <div class="thumbnail"> <img src="{{ category.picture }}" /> <div class="caption"> <h4 class="pull-right">${{ category.price }}</h4> <h4><a href="#">{{ category.itemname }}</a> </h4> </div> </div> </div> {% endfor %}
models.py:
from django.db import models class storecategory(models.model): categoryname = models.charfield(max_length=128) def __unicode__(self): return self.categoryname class storeitem(models.model): category = models.foreignkey(storecategory) itemname = models.charfield(max_length=128) description = models.charfield(max_length=1024) price = models.decimalfield(max_digits=16,decimal_places=2) quantity = models.integerfield(default=0) picture = models.imagefield(upload_to='store_images', blank=false) def __unicode__(self): return self.itemname
urls.py:
urlpatterns = patterns('', url(r'^$', views.webstore, name='webstore'), url(r'^fiberarts/$', views.fiberarts, name='fiberarts'), url(r'^fiberarts/(?p<directory>[\w]+)/(?p<image_name>[\w]+)$',views.getimage) )
view: ( never called, problem regex?)
def getimage(request, directory, image_name): imagelocation = directory + '/' + image_name print imagelocation image_data = open(imagelocation, "rb").read() return httpresponse(image_data, mimetype="image/png")
edit: here error i'm getting:
[06/apr/2014 15:48:35] "get /webstore/fiberarts/store_images/product_150_1.jpg http/1.1" 404 3743
change url regex (?p<directory>[\w]+)/(?p<image_name>[\w]+\.[\w]+)$'
, fix view accept right number of arguments. should receive right image_name
inside view.
Comments
Post a Comment