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.)
Related
Background. We want our users to check if the metadata we read out of several images is correct.
Therefore, we show the retreived data in a formset to let the user check everything.
In case the metadata has errors (that happens) we would like to display them as form errors here.
Problem. To obtain the error messages we need bound forms. Feeding our various data into a bound ModelFormSet and taking care of the ManagementForm is ... not so simple. With the unbound version everything works fine.
Idea. If we could produce an unbound form and submit it within the python code, that would be a simple way to get the POST data we need to create the bound form.
I have a question about REST design in general and specifically what the best way to implement a solution is in Django Rest Framework. Here it the situation:
Say I have an app for keeping track of albums that the user likes. In the browser, the user sees a list of albums and each one has a check box next to it. Checking the box means you like the album. At the bottom of the page is a submit button.
I want the submit button to initiate an AJAX request that sends tp my API endpoint a list of the ids (as in, the Djano model ids) of the albums that are liked by the user.
My question is, is this a standard approach for doing this sort of thing (I am new to web stuff and REST in particular). In other words, is there a better way to handle the transmission of these data than to send an array of ids like this? As a corollary, if this is an alright approach, how does one implement this in Django Rest Framework in a way which is consistent with its intended methodology.
I am keeping this question a little vague (not presenting any code for the album serializer, for example) intentionally because I am looking to learn some fundamentals, not to debug a particular piece of code.
Thanks a lot in advance!
Consider the upvote button to the left. When you click it, a request may be sent to stackoverflow.com/question/12345/upvote. It creates an "action resource" on the db, so later you can go to your user profile and check out the list of actions you took.
You can consider doing the same thing for your application. It may be a better user experience to have immediate action taken like SO, or a "batch" request like with gmail's check boxes.
I'm working on a project that's in the process of migrating from Django 1.3 to Node/Backbone.js
Consequently, I'm working with two sets of views. I have a function for validating and processing images to be uploaded via a form, written in Python using PIL, that was originally designed for use in the Django views; however, the view that handles forms has since been migrated to Backbone.
I don't expect it to be an optimal solution, but for the sake of expediency I wondered if there's a way to call this as a standalone Python script (just to do the image processing) within the Backbone form view?
The crux of the problem is that the new JavaScript view is updating the database via PUT, whereas the original Django code used POST. I'm trying to avoid using two separate views on the same form and commingling the two methods, since the JavaScript view is perfectly capable of handling the file upload itself; however, I would like to avoid having to rewrite the imaging script using a new library.
From looking at some other threads, it seems that making an AJAX request might be the way to go, e.g. something along these lines, but the only examples I've found use jQuery, whereas I feel like there should be a similar way of doing it using Backbone's built in AJAX functionality.
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.
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.