On my website, I am utilizing django-ckeditor to allow users to input rich text content. Each webpage on the site represents a unique document identified by an id.
For instance, two different documents will have separate webpages with URLs like -
example.com/documents/doc1 and example.com/documents/doc2
Both of these web pages contain multiple CKEDITOR instances. My goal is to direct images uploaded through CKEDITOR on webpage example.com/documents/doc1 to a specific directory
/media/uploads/doc1/
and images uploaded through CKEDITOR on webpage example.com/documents/doc2 to another directory -
/media/uploads/doc2/
However, an issue arises in the upload view method in views.py of the django-ckeditor module -
def post(self, request, **kwargs):
"""
Uploads a file and sends back its URL to CKEditor.
"""
# Get the uploaded file from the request.
upload = request.FILES['upload']
# Verify that the file is a valid image.
backend = image_processing.get_backend()
try:
backend.image_verify(upload)
except utils.NotAnImageException:
return HttpResponse("""
<script type='text/javascript'>
alert('Invalid image')
window.parent.CKEDITOR.tools.callFunction({0});
</script>""".format(request.GET['CKEditorFuncNum']))
# Open output file in which to store the upload.
upload_filename = get_upload_filename(upload.name, request.user)
saved_path = default_storage.save(upload_filename, upload)
if backend.should_create_thumbnail(saved_path):
backend.create_thumbnail(saved_path)
url = utils.get_media_url(saved_path)
# Respond with Javascript sending ckeditor upload URL.
return HttpResponse("""
<script type='text/javascript'>
window.parent.CKEDITOR.tools.callFunction({0}, '{1}');
</script>""".format(request.GET['CKEditorFuncNum'], url))
The upload_filename is generated as get_upload_filename(upload.name,request.user)
This function takes the filename and request.user as inputs and saves files in user-specific folders. How can I pass the document id from the URL to the post request so that I can utilize it to save images in document-specific directories?
If further clarification is required, please leave a comment below, and I will provide additional information. Thank you.