django-ckeditor change upload path prefix - django

I want to change the prefix of upload path in django-ckeditor. By default it generates subdirectories by using username and date so something like:
/media/uploads/username/year/month/day/uploaded_file
The documentation says:
Set the CKEDITOR_RESTRICT_BY_USER setting to True in the project’s settings.py file (default False). This restricts access to uploaded images to the uploading user (e.g. each user only sees and uploads their own images). Upload paths are prefixed by the string returned by get_username. If CKEDITOR_RESTRICT_BY_USER is set to a string, the named property is used instead.
After few tries, still can't figure out how to configure this to handle prefix of uploaded files.
Thanks for any help.

Just set CKEDITOR_RESTRICT_BY_USER = True in your settings.py file.
The upload path will be /media/uploads/username/year/month/day/uploaded_file as you desired.
Ckeditor processes the user path using following view. https://github.com/django-ckeditor/django-ckeditor/blob/master/ckeditor_uploader/views.py

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-CKEditor Image Upload File Types

I have successfully installed Django CKeditor with Uploader but I have one problem I cannot find information about. When I select Upload I get the standard Windows file browser but it is set to all files. How can I configure CKUploader to just show supported image file types?
Thanks.
You can restrict upload to image only by setting CKEDITOR_ALLOW_NONIMAGE_FILES = False in settings.py

How to get data from config file (config.json) in real time in settings.py file in django

I have a project made in Django. I have only added social auth for login purposes. I want selected emails only to log in to the website. I used social-auth-app-django library for social auth and added a variable SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_EMAILS to the settings.py file where it contains a list of all the emails permitted for logging in.
My project directory looks something like this:
project_parent/
----project/
--------settings.py
--------wsgi.py
--------asgi.py
--------urls.py
----app/
--------models.py
--------views.py
--------urls.py
--------admin.py
----db.sqlite3
----manage.py
----config.json
Here is the config file:
{
"OAUTH2": {
"WHITELISTED_EMAILS": [
"xyz#gmail.com",
"abc#gmail.com",
"efg#gmail.com",
"lmn#gmail.com"
]
}
}
In settings.py file I have loaded the config file like this:
config = open('./config.json',)
data = json.load(config)
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_EMAILS = data['OAUTH2']['WHITELISTED_EMAILS']
I have made a webpage that takes the new mail id (need to add to the config file) and appends that particular mail id (or a list of mail ids) to the config.json file. But now the problem arises, the newly added mails don't reflect directly to the variable defined in the settings.py file. For that, I need to restart the code in order to get the latest config file. Every time I add a mail id, I need to restart the code.
I thought to make a database table in my app folder and load that table in settings.py by import the model from app folder. But on importing, the terminal raises the error as it says app is still not loaded so that I can't use the models inside my app.
Is there a way in which I can directly load the database table without importing the models.py file from app folder? Or if possible to load the config.json file in real-time so that I don't have to restart the code
settings.py in Django is loaded once at execution time, we should not change the values during runtime. Although that is suggested by the docs there are ways you can change the settings values during runtime. You can use django-constance library, and then make a form to update the setting's value during runtime by editing the database value.

Pillow upload path in pythonanewhere?

I'm using pythonanywhere.com and would like to attach images for my posts. When installed my model, I have a folder path for downloads like this
image = models.ImageField(upload_to="/static/uploads/",
but when I attach the photo I receive a message
The joined path (/static/uploads/1.jpg) is located outside of the base path component (/home/farmville)
if I specify the full path
/home/username/project/static/uploads/
it's working, but in the template file this path does not work, the image is not :(
How can it can be solved?
The two paths are different things, so using the same path doesn't make sense. The upload_to path is a path to a directory on the server where the image should be uploaded. The path in the template is probably a URL path (it's hard to tell without the actual template) and so it needs to point to where you're serving the uploads from.

Django How to allow apache to create directories?

I am making a django application that makes user of django's ImageField to upload a file to a specific folder. I am using this field for storing the user's profile pictures. But the problem is the path that I give to upload_to is dynamic and depends on user and will create directories if needed. i.e if the path is user/1/profile-pic/large/pic.jpg, it will create the directores, user/, user/1/ so on, if the are not already there. It worked fine in development. But now when I have put my website on a VM and serving it using apache. Django raises the permission denied error. As I have to make directories dynamically so I can't make them ahead of everything and change their permissions. So I was wondering if there is any of way of acoomplishing it.
You should chown you media directory to the user which runs you django app.