I figure the title is somewhat confusing but that's the best I came up with.
I have an upload image form which I want to process the image file further after the user upload it (with the user - cropping using jquery) rather than put it on the model imagefield right away.
Meaning, I need to save the image temporarily so I can present it to the user in a template with the jquery crop plugin.
My current solution is ugly, I made a model called TempImageToProcess which I save the image to and from that I re-present it to the user to further process.
So my question is: is there any elegant way to save image temporarily in Django?
You should check TemporaryFileUploadHandler in this section.
Look this SO question for a clean up afterwards if any unexpected error occur or validation errors.
Hope this will lead you some where.
Related
I'm working on this project of movie database, I made database of the movie details(Name, Rating... etc) But now I need to save the Image in somewhere I saved it in a folder but if the user is not a developer how can he access the folder? Or is there any way for me to save the Images on a database? Flask uses SQLAlchemy and you can't save images in it and if you can it's not that good. If my question is not clear please let me know.
There are a couple of good answers on this post which deals with a similar issue.
Correct way to declare an image field, sqlalchemy
To summarise, either use the SQLAlchemy-ImageAttach library, or have an "image field" on the model, which is actually just a String URL to where the image is saved.
I've been working on Django project. Currently, user can upload only one image but now I want to change it so user can upload some images.
models.py is like this.
class Entry(models.Model):
photo = models.ImageField(...)
I am thinking of just adding photo2 and photo3 inside Entry model. But I'm wondering if there's a better way to do it. I don't want to delete images that are already uploaded. Anyone who could give me tips? Also, I don't like the file upload form's design and some people use just button-like form. I also want to know how to create button-like form.
Create new model called 'Photo'.
Create relationship from 'Entry' to 'Photo'.
If you want to use photos that related with Entry record, you need to use select_related() function
Also you can find button-like upload form here.
I have existing Django-based project with some files uploaded.
I need to add a feature to automatically resize new uploaded files to some resolution (200x200). I found a nice library django-stdimage that does what I need.
But on upload it stores original file with its original resolution. And existing code works with the original file instead of resized one.
class Product(models.Model):
name = models.CharField(verbose_name=_('Name'), max_length=64)
image = StdImageField(upload_to='product_images/', verbose_name=_('Image'), blank=True, null=True,
variations={'default': (200, 200)})
I would like to save processed files by the same name as original file. I do not need original file by the way.
I do not want to change all the code where it works with image field - there are complex DRF serializers, some views, forms, templates, etc.
So I would like to get new resized image as before by using myproduct.image - in templates for example.
Is it possible to do without subclassing StdImageField ?
I opened a ticket on the StdImage's page and received an answer..
No, I'm sorry, but nether would this work, nor would I advice you to
do such a thing. It would add implicit behavior. That's something you
might want to avoid long term.
You can always overwrite the field tho and implement your own
behavior. The StdImage code base should be good guidance to implement
your own behavior.
I've been trying to get my head around uploading image files in Django. There are several tutorials that I have followed and they do help a bit, for example this one and the one here.
While they do give a basic idea of what is happening/how to think about it, I am not yet able transfer that to ImageKit implementation of ProcessedImageField.
My problem is the following: I have several ProcessedImageFields. To upload new pictures, I use a form with a slightly modified ClearableFileIput widget, which displays a thumbnail above the standard 'Choose file' button. This works absolutely fine, the user is able to select a couple of files(one for each field), upload them all at once when the form is saved. I use S3 for storing data using django-storages.
I would like to be able to add an ajax upload functionality on those so that image processing could be delegated away from the form saving. When the user chooses the image, it should be uploaded and its updated once the file is processed. Then changes to the model should only be preserved if the user actually saves the form, otherwise new files should be discarded.
Any ideas where to start looking? Perhaps there are projects that use Django-Imagekit that use one of the existing django ajax libraries?
ProcessedImageFields are really just normal Django ImageFields that perform the image processing (synchronously) before saving the image, and Django ImageFields are really just specialized FileFields. So, if you're using ProcessedImageFields, there's nothing different than normal form file handling; any non-Django specific tutorial about AJAX uploads should apply equally.
As far as processing the image asynchronously, there are two approaches you could take.
The first is to do one form submission with the image and return an id for that image which could be sent with a second submission for the rest of the form. In this case, the processing of the image is done synchronously on the server side, but the user can do other thing with your app while it happens. This makes the final submission shorter because the file has already been sent, but the image processing still needs to complete before the form is successfully saved. You might try looking at jQuery-File-Upload's Django examples, though I'm not sure how good or relevant they are.
If, on the other hand, you want to actually do the image processing asynchronously on the server, it's a little more complicated. One approach would be to have two image separate fields on your model—one for the raw image and one for the current, processed image. In the view that handles the form submission, you'd save the raw image in the model and kick off an asynchronous task (using Celery or something similar) that processes the raw image and then saves it to the other field.
As for the discarding of new files, you'll probably have to do that in a cleanup script that runs periodically. (What you're asking is that the image be preserved on the server, but then discarded when it's not saved, but the server can never be reliably notified of the form not being saved.)
I'm using django. I want a user to upload an image for a user post, but I'm not sure the backend of this. Should I setup a db with the url of the image, a folder for the user, a folder inside of the post, and the image finally in that folder? What is the best (fastest, efficient, nonconfusing) way of doing this?
You can use the built in django ImageField. This essentially is set up to store and reference a url relative to a media dir on the webserver.
There is a pretty basic example here.
EDIT:
For your own implementation outside of django most people would implement it in a similar way to how Django's imagefield works. Basically, you story a reference to a file in a filesystem somewhere, and store the actual file on the filesystem.
You can store the actual image in the database but I think most people prefer to not store it in the database. This stackoverflow question has a lot of info about why one would want to do it one way or another. I myself have done this both ways and like storing them in the filesystem more than in the database in most cases.
You can use the ImageField which comes built in in with django. The good thing about this it stores and manages it within Django, you can resize and get url to the image all using PIL and Django helper methods.
This is the best way for deployment as well, once you decide to deploy you will be able to tweak the system to best serve up static files, as supposed to managing it yourself.
Goodluck.