简体   繁体   English

在django-ckeditor中上传图像时,如何在请求中发送其他数据?

[英]How to send additional data in request while uploading images in django-ckeditor?

I am using django-ckeditor for users to enter rich text on my site's webpages.Every webpage on my site represents a document with a unique id. 我正在使用django-ckeditor来使用户在我的网站网页上输入富文本。我网站上的每个网页都代表一个具有唯一ID的文档。

For ex- two separate documents will have two web pages with URLs such as - 例如,两个单独的文档将具有两个网址,例如-

example.com/documents/doc1 and example.com/documents/doc2

Each of these 2 web pages have multiple CKEDITORs.I want that when user uploads image through CKEDITORs on webpage- example.com/documents/doc1 should go to a separate directory 这2个网页中的每个网页都有多个CKEDITOR。我希望当用户通过网页上的CKEDITOR上传图像时,example.com / documents / doc1应该转到单独的目录

/media/uploads/doc1/ 

and images uploaded through CKEDITORs on webpage example.com/documents/doc2 should go to a separate directory - 和通过example.com/documents/doc2页面上的CKEDITOR上传的图像应转到单独的目录-

/media/uploads/doc2/

Now the problem arises here.In the upload view method in views.py of django-ckeditor module - 现在出现问题了。在django-ckeditor模块的views.py中的upload view方法中-

def post(self, request, **kwargs):
    """
    Uploads a file and send back its URL to CKEditor.
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    #Verify that 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 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))

upload_filename is defined as get_upload_filename(upload.name,request.user) upload_filename定义为get_upload_filename(upload.name,request.user)

It takes filename and request.user as parameters and stores files in user specific folders.How can I pass the docid from my url to the post request so that I can use docid to store images in the document-centric folders? 它以filename和request.user作为参数,并将文件存储在用户特定的文件夹中。如何将docid从url传递到发布请求,以便可以使用docid将图像存储在以文档为中心的文件夹中?

I hope my point is understood.If any more information is needed then please let me know in the comments, I would add those. 我希望我的观点能被理解。如果需要更多信息,请在评论中让我知道,我会补充一下。 Thanks 谢谢

将其添加到config.filebrowserUploadUrl中:

config.filebrowserUploadUrl = "/upload.php?docid=" + docid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM