python 3.3 - Django - Images not displaying properly -
currently working on small django e-commerce project , run small issue. reference, using tango django book available online free.
i have imagefield set in models in order specify image product model of product. have model called category referenced product model using foreignkey.
the image uploads /static/products_img folder in model, however, when image displayed in template, image links /static/static/products_img folder. however, when remove /static in model, , upload image through admin interface, uploads /products_img , not static folder, in of course displays fine. but... gets weirder, when delete newly created directory , place image in /product_img folder in static folder, still displays!
i'm confused.
models.py class product(models.model): ... image = models.imagefield(upload_to='static/products_img', blank=true) template.html <a href="{{ product.slug }}"><img src="{{ static_url }}{{ product.image }}" alt="{{ product.name }}" /></a> settings.py ... settings_dir = os.path.dirname(__file__) project_path = os.path.join(settings_dir, os.pardir) project_path = os.path.abspath(project_path) ... static_path = os.path.join(project_path,'static') static_url = '/static/' staticfiles_dirs = ( static_path, )
i think have confusion between static , media files. static files bundled apps (css, js...), while media files uploaded users.
from understand, products images media files, not static files.
what if replace template part with:
<a href="{{ product.slug }}"><img src="{{ product.image.url }}" alt="{{ product.name }}" /></a>
and model :
image = models.imagefield(upload_to='products/img', blank=true)
and add corresponding settings settings.py :
project_path = os.path.abspath(os.path.dirname(__file__)) # this, media directory same directory settings.py file # can use standard path "/var/www/media" # important : in case, have create directory hand media_root = os.path.join(project_path, "media") media_url = "/media/"
if upload image product, should saved under media_root
directory. url displayed in template should /media/projects/img/image_name
.
Comments
Post a Comment