permission for show media folder images in django, python - python

I want set permission for show media folder images. for example, I want show my images to users who are staff. When user in not staff, show 404 Error.
Thanks

Related

Reading all the image files in a folder in Django

I am trying to create a picture slideshow which will show all the png and jpg files of a folder using django.
Problem is how do I open windows explorer through django and prompt user to choose a folder name to load images from. Once this is done, how do I read all image files from this folder? Can I store all image files from this folder inside a list and pass this list in template views through context?
This link “https://github.com/csev/dj4e-samples/tree/master/pics”
shows how to store data into to database(sqlite is the database used here) using Django forms. But you cannot upload an entire folder at once, so you have to create a one to many model between display_id(This is just a field name in models you can name it anything you want) and pics. Now you can individually upload all pics in the folder to the same display _id and access all of them using this display_id. Also make sure to pass content_type for jpg and png separately while retrieving the pics.

Path to static files in Django CMS admin

I'm having an issue with some administrative icons being used in a Django CMS I have deployed for one of my clients. As you can see from the attached image, the path to the static folder where the images are stored is wrong and therefore doesn't render the icons. It's missing the static part of the URL. Can anyone provide any pointers on this?
Many thanks in advance for any assistance people may be able to provide on this
In production go to,
/etc/nginx/sites-available/django
Remove or comment these lines
#location /static/admin {
# alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
#}
You can have a look on my website for this.

django - images system like wordpress

I am impressed with how wordpress handle the images (where user can upload and insert images into post). I want to make this feature to able applied in django. How do I achieve it?
Basically I have two models :
1.post - for writing the content
2.media - for uploading images/files
But in the post model I would like to able insert the images from the media model in the textField(). For the textField I am using django-tinymce for rich text context with filebrowser integration. So, better use the Image button to upload/insert image to/from media model.
Also the user only allow to browse the images that he/she uploaded in the filebrowser.
Lastly beside in the admin, this feature also need to work in frontend.
So is this possible?

I need some advice about static and media files

I'm starting to program in Django and need some advice from you.
My project is a catalog containing more than 1000 products. Each product has an image.
I need to optimize the loading time and management of these images and my question is: Where do I store these images? In static folder, in the media folder or another solution?
I read the Django documentation and searched on google on static files and media files, but only found recommendations on "store user uploaded data such profile image in Media folder". But I do not know if this recommendation would apply to my project to optimize loading time and management of these image files.
What are your thoughts on the best way to store these images in my project based on your experience?
I'm using Nginx, DigitalOcean VPS.
Thank you!
Static files are part of your theme / skin ("packaging"). These files should generally not change and should be served based upon when the browser last retrieved them and set far ahead in the future:
location /static {
expires max;
}
Media files are part of the content, not the packaging of the site. It's much more likely that an image changes: the first one uploaded doesn't look that good, better manufacturer supplied media, etc. So it makes more sense to base it's expiration upon the modification date:
location /media {
# Optional: if rollbacks are frequent
# if_modified_since before;
expires modified+1w;
}
Regarding the rollbacks: if it's likely that older versions of an image is restored from backup or versioning systems, you'll want to set this. Note that Django will not set the modification time of an uploaded image to the modification time on the uploader's computer, so a new upload will be a new image, even if it's a previous version.
Now here comes the tricky part: page optimization tools will tell warn you about the expiration time of images not being far in the future and thus will complain about the media files. This is because they can't make a distinction between packaging and content when it comes to images.
Another thing to note is that after that week, the browser will request the images each time and nginx will serve a 304 response when it hasn't changed. Thus your page will generate many more requests. This and the page optimization tools are the two reasons why modified isn't used much for images in the wild and instead only the first policy is used. To deal with modified images, you will then associate a differently named image with the same resource (product) and thus is the responsibility of the content managers or a middleware layer that renames uploaded images to a unique name. One simple trick is to name media images as their md5 or sha1 hash.
Even if you apply a single expiration policy for serving media and static, I would still serve them from different directories. The reason is that static files do not have to be writable by the user running Django (only for the user running the collectstatic management command). This prevents configuration errors or a compromized Django user from messing with the static directory.

Multiple pdf upload from django admin

I am creating an application in django where I want to upload multiple file from django admin. I also want these files to be associated with a particular user in my database. for e.g a pdf file will have a file names as 'john.pdf', 'matt.pdf', 'alice.pdf' and I want to upload all these files at once from django admin and each file should be associated with particular user, so if user john logs in he can see pdf 'john.pdf' in his profile page.
I am new to django and web programming and I have been banging my head for a couple of days but I just cannot find the right logic to implement such a code.
I have looked in these resources but I still cannot really find an answer
How to upload multiple file in django admin models
How to upload through django admin.
I am using django with mysql database. I highly appreciate and thank anyone in advance who could help me out with this problem. (hoping not to get downvoted too much)
Ok so I found out a solution but I still don't know if thats the best solution there. What I did was to add path of my pdf files in MySQL database and add attributes to those paths. I wrote a script in python to add those files to MySQL database. My script listed all the files in the directory, where the files were stored and added them to the database. From these attributes I could retrieve those paths. As these are just paths to static files I had to save them in the static folder for django to access and display them and the rest was simple.

Categories