django - Upload a folder under media - django

I'm trying to upload an entire folder (of images) inside the media_root in django.
I want something like im = models.ImageField(upload_to="images/", blank=True, null=True) but I do not want to upload many images one by one.
How can I do that? There is a simple way to use FileField or ImageField that upload directly the folder instead of a file/image?

I've done some digging around and it doesn't seem possible. According to this answer. You can use some JS to select all the files in the directory you want to upload and then create an upload field for each of the files.

Related

Cloudinary upload image from Django admin panel from two or more databases

everyone. I decide to use Cloudinary for storing images.
This is my model field with image:
avatar = models.ImageField(upload_to='avatar/', default='avatar/default.png', blank=True)
All works fine for me, but I have one little issue.
When I uploaded the image from admin panel, cloudinary upload it to my cloudinary folder 'avatar' with some modified name, for example: 'july2022_kda4th' or 'july2022_aidkdk'
But when I uploaded this image from admin panel of another database (production), cloudinary upload the image with another name. So, I have two similar images in cloudinary. It's not convenient.
How can I fix it?
By default, if you don't supply a public_id in the upload API call, a random string is assigned to the asset. You can read more here: https://cloudinary.com/documentation/upload_images#public_id.
It sounds like you want to use the asset filename as the public_id so what you can do is:
In forms set use_filename=true and unique_filename=false as described in this link
OR if above is not working you can
Create an upload preset https://cloudinary.com/documentation/upload_presets
Enable Use filename or externally defined Public ID: option
Disable Unique filename:
Set this preset as the default upload API/UI (https://cloudinary.com/documentation/upload_presets#default_upload_presets) or include this upload_preset in your upload call

Django save large files

I'm creating a website where the user is able to upload large files. Around 100-300Mb. I'm using django but I want to know what is the best option to save the files. Should I just add them to my user database? Or should I create a media folder and save the files there. If so then this would mean that I have to save the name of the file in my user database?
Please let me know how you would tackle this issue.
django does not store files in the database, but in media folder. The FileField contains the path to the file(in the media folder). You can change that behavior but it is not recommended(as general practice). What you should consider however is that files of that size will require some more work both in django and the front-end since the upload process will freeze the server. A potential solution to the problem is this: https://pypi.org/project/django-chunked-upload/

Is an uploaded image, a static file in Django?

I am developing an app in Django 1.6 and would like to know if the photos that I upload via the admin interface are "static files" in Django terminology.
I have this model:
from django.db import models
class ShowroomDetail(models.Model):
title = models.CharField(max_length=1000)
description = models.CharField(max_length=4000)
class ShowroomPhoto(models.Model):
showroom = models.ForeignKey(ShowroomDetail, related_name='photos')
photo = models.ImageField(upload_to='images/')
which I using as a basis of developing a page where there can be one or more images displayed along with the title and description. The images will only ever be uploaded by the admin interface and more photos for the page may be added at a later date.
So are these uploaded photo's "static files"?
No. An image uploaded through the website will be stored in the MEDIA_ROOT folder. It is considered then as a media file and not a static file. For your example, if MEDIA_ROOT = '/path/to/project/media/, then your photos will be stored in media/images/.
The difference is staticfiles will be collected for each application and are part of the code. Typically it is image for the design, CSS files, Javascript, etc. They are necessary to run the project.
Media files are files uploaded through the administration or by user through a special application to add content (avatar, gallery image, message attachment...). This content is independent of the code of the project and is considered as content.
Related: What is the difference between static files and media files in Django?
"static files" as a term of art refers to the static files, and the location you configure for it. Uploaded files could be found by static files if you upload them to a location which static files uses. Obviously, security issues abound.

Uploading files to amazon S3 and other fields to local server using Django

I'm writing a project in Django that has user uploaded images related to products, so my product model is something like:
class Product(models.Model):
name = models.CharField(max_length=128)
description = models.CharField(max_length=255)
owner = models.ForeighKey(User)
image = models.ImageField()
I would like to store the images at Amazon S3, since it's pretty cheap and fast. I would, as others, like to avoid the overhead of uploading the file to my server and then to S3. There are some sample code explaining how to upload a file directly to S3 from the client browser.
The issue I see (and I did not find any solution yet) is that I don't have only an image to upload. My object (Product) also has other fields, such as name, description and so on. All the examples I saw use one form for the image only. I would like to have one html form (with image, name and so on) for the user and once the user click on "submit", I would be able to store the data in amazon S3 and then the other info (name and so on), on my local database.
According to the image in http://docs.amazonwebservices.com/AmazonS3/latest/dev/UsingHTTPPOST.html using a POST method, it's possible to send data from the client to amazon S3 and my webserver. It's now clear if both situations can be included in the same page (same html form).
According to Amazon docs, It's possible to upload a file and set the redirect ULR when the upload is successful.
1) Is it possible to upload the image to the URL and then redirect to my webserver to store the rest of the information? Does the redirect keep the POST data (name, description etc)? How can I access the name of the file stored in S3?
2) Is there any other way to achieve my goal (besides my option #1)?
Thanks in advance
Checkout django-storages (http://code.welldev.org/django-storages/) it does exactly what you need.
And you don't have to care about multiple forms/redirects etc etc, it will replace the default file storage backend and just push files to amazon s3 bucket.
It supports several kind of storages and S3 in one among them, I've used for several projects so far and it's really easy to plug in.
Since docs seems pretty dead, here's my configurations:
settings.py
DEFAULT_FILE_STORAGE = "storages.backends.s3boto.S3BotoStorage"
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
AWS_STORAGE_BUCKET_NAME = ''
and of course you need to install django-storages

Django Bulk Zip upload

I want to upload images to a gallery app. I want the user to be able to either load images normaly, or upload on zip file containing all the images for that gallery. Then it must be uncompressed and all images must be added to that model. This is for the admin site.
Any ideas?
You could either use the existing django-app django-photologue which enables you to do that or have a look at how it is implemented there: https://code.google.com/p/django-photologue/source/browse/trunk/photologue/models.py.
If you see that photlogue is lacking some of the functionality you need, you could also subclass and extend photologue's models in your app!