python - Saving Image to Django, the correct way ? -
what best practice set upload path in django 1.4? have model
def upload_image(instance, filename): return os.path.join(instance.slug, filename) class book(models.model): ... ... image = models.imagefield(upload_to=upload_image) def admin_image(self): return "<img src='%s' />" % self.image .... with settings.py
project_dir = os.path.dirname(os.path.realpath(__file__)) root_dir = project_dir.rsplit(os.sep, 1)[0] media_root = os.path.join(root_dir, 'books', 'static', 'books') media_url = '/media/' static_root = '' static_url = '/static/' and admin.py
class bookadmin(admin.modeladmin): list_display = ('title', 'admin_image') so, in end, got image correctly saved /root-of-project/books/static/books/programming-java/wall.jpg, however, saved path in database "programming-java/wall.jpg" , have concat prefix '/static/books/' everytime displaying image
why concat/hardcode lines in templates? can use static tag purpose
{% load staticfiles %} <img src="{% static "images/hi.jpg" %}" /> alias
{% load staticfiles %} <img src="{% static object.image.url %}" /> and if must, can add method model you?
Comments
Post a Comment