Django Temporary Images

Hi i have this type of model

class Post(models.Model): title = models.CharField() image = models.ImageField() # other fields 

To create a new message, I would do this thread:

  • on the new page 1, the user selects a photo and uploads it (Submit)
  • on the new page page 2, the user can see the photo and fill in other fields, such as "title"

My question is how to upload a photo and display it on the next page without creating a new Post object with an image field. Should I save the image in a time series directory? In this case, how can I save the link and use it on the second page?

Thanks.

+4
source share
3 answers

There are several solutions.

  • Define another model to just hold images, and add might be OneToOneField in your Post model. When submitting page1, create an instance of this image model and place it on page2 as the image field.
  • With the html5 API, you can access the local file in the image field. Thus, you can display the image on page2 using the local path on the client side, without saving it to the server. refer: View image selected from client-side file system before uploading?
+1
source

First read the relevant document so that you understand what happens with the downloaded files: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/

The simplest IMHO solution would be

1 / save the downloaded file in some temporary directory - just make sure that you can serve the content one way or another (either directly through the web server or through the user view, if you want more control over who can access this content),

2 / pass the path to the temporary file and the URL to the β€œnext” view one way or another (hidden form fields, session ...).

0
source

I use a table to store images (virtually any files :)). Each image is associated with an object (FK) and has the status - Draft, Approved, Deleted. Therefore, I do not use a temporary directory, I just change the status in the image table.

0
source

All Articles