【问题标题】:How to send additional data in request while uploading images in django-ckeditor?在 django-ckeditor 中上传图像时如何在请求中发送附加数据?
【发布时间】:2015-09-22 13:30:15
【问题描述】:

我正在使用 django-ckeditor 让用户在我网站的网页上输入富文本。我网站上的每个网页都代表一个具有唯一 ID 的文档。

例如,两个单独的文档将有两个带有 URL 的网页,例如 -

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

这两个网页中的每一个都有多个 CKEDITOR。我希望当用户通过网页上的 CKEDITOR 上传图像时 - example.com/documents/doc1 应该转到单独的目录

/media/uploads/doc1/ 

并且通过网页example.com/documents/doc2上的CKEDITORs上传的图像应该转到一个单独的目录 -

/media/uploads/doc2/

现在问题就出现在这里了。在django-ckeditor模块的views.py中的上传视图方法中——

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 定义为 get_upload_filename(upload.name,request.user)

它将文件名和 request.user 作为参数并将文件存储在用户特定的文件夹中。如何将 docid 从我的 url 传递到 post 请求,以便我可以使用 docid 将图像存储在以文档为中心的文件夹中?

我希望我的观点被理解。如果需要更多信息,请在 cmets 中告诉我,我会添加这些信息。谢谢

【问题讨论】:

    标签: javascript django ckeditor image-uploading django-ckeditor


    【解决方案1】:

    在config.filebrowserUploadUrl中添加:

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

    【讨论】:

    • 我使用的是 django 后端,而不是 php 后端
    • 然后用upload.py代替upload.php你真的要我猜你在CKEditor配置中使用的url吗?
    猜你喜欢
    • 2015-10-07
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多