imagefield

Django - Display ImageField

痞子三分冷 提交于 2019-11-27 23:31:09
i just start to use django and i haven't found a lot of info on how to display an imageField, so i made this: models.py class Car(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(max_digits=5, decimal_places=2) photo = models.ImageField(upload_to='site_media') views.py def image(request): carx = Car() variables = RequestContext(request,{ 'carx':carx }) return render_to_response('image.html',variables) image.html {% extends "base.html" %} {% block content %} <img src=carx /> {% endblock %} I already save an image since terminal and i know is there, also if a do

Replacing a Django image doesn't delete original

邮差的信 提交于 2019-11-27 11:25:27
问题 In Django, if you have a ImageFile in a model, deleting will remove the associated file from disk as well as removing the record from the database. Shouldn't replacing an image also remove the unneeded file from disk? Instead, I see that it keeps the original and adds the replacement. Now deleting the object won't delete the original file only the replacement. Are there any good strategies to doing this? I don't want to have a bunch of orphan files if my users replace their images frequently.

Image file not deleted when object with ImageField field is deleted

佐手、 提交于 2019-11-27 03:18:59
问题 I've got a model Picture with an ImageField . When deleting an instance of Picture , the file set in the ImageField is not deleted. Is it a bug? How can I do it? 回答1: Deletion of files associated with FileField s and ImageField s was intentionally removed in Django 1.3. See ticket #6456. Django uses transactions extensively to prevent data corruption if something goes wrong with the request. If a deletion transaction is rolled-backed, after the file has been deleted from the filesystem, then

Override django's model delete method for bulk deletion

戏子无情 提交于 2019-11-27 01:37:33
问题 I'm overriding Django's model delete method in order to delete orphan files in the disk for image fields, something like this: class Image(models.Model): img = models.ImageField(upload_to=get_image_path) ... def delete(self, *args, **kwargs): self.img.delete() super(Image, self).delete(*args, **kwargs) This works fine when I delete single objects from the admin, but when I select multiple objects and delete them, this doesn't seem to get called. I have been googling for a while but haven't

How do I get Django Admin to delete files when I remove an object from the database/model?

感情迁移 提交于 2019-11-26 04:37:29
问题 I am using 1.2.5 with a standard ImageField and using the built-in storage backend. Files upload fine but when I remove an entry from admin the actual file on the server does not delete. 回答1: You can receive the pre_delete or post_delete signal (see @toto_tico's comment below) and call the delete() method on the FileField object, thus (in models.py): class MyModel(models.Model): file = models.FileField() ... # Receive the pre_delete signal and delete the file associated with the model