ckeditor_uploader动态图像上传路径 [英] ckeditor_uploader Dynamic Image Upload Path
问题描述
我有一个Django应用程序,要求用户为每个文档上传多个图像。该应用程序已安装django-ckeditor,但是上载的图像最终位于 CKEDITOR_UPLOAD_PATH
设置所设置的文件夹中。我希望该路径基于URL方案是动态的。
I have a Django application that requires users to upload multiple images per document. The application has django-ckeditor installed, but the uploaded images end up in the same folder set by the CKEDITOR_UPLOAD_PATH
setting. I would like the path to be dynamic, based on the URL scheme.
例如,将图像上传到 https://的CKEditor实例上myapp / report / 1 / finding / 5
应该位于 /ckeditor_base_path/report/1/finding/5/my_img.png
中。
For example, images uploaded to the CKEditor instance on https://myapp/report/1/finding/5
should reside in /ckeditor_base_path/report/1/finding/5/my_img.png
.
不幸的是,用于处理设置路径的视图函数的URL是由小部件设置的( CKEditorUploadingWidget )在视图创建上下文
参数之前。
Unfortunately, the URL for the view function that handles setting the path is set by the widget (CKEditorUploadingWidget) before the view creates a context
parameter.
我希望将参数URL参数发送到 ImageUploadView 进行处理。
I was hoping to send the parameters URL parameters to ImageUploadView for processing. Any help or advice on this is greatly appreciated.
推荐答案
如果它可以帮助其他任何人,这就是我所做的。用户浏览时,由于它们特定于报告的那部分,因此将仅限于包含查找图像的文件夹。同样,上传图像会将其发送到同一文件夹。
In case it helps anyone else, here's what I did. When users browse, they will be restricted to the folder with the finding images, since they are specific to that part of the report. Similarly, uploading an image will send it to the same folder.
总而言之,您必须:
- 将CKEditor Uploader URL指向您的视图版本
- 通过相应的表单视图更新CKEditor Uploader小部件
- 覆盖CKEditor上传器
ImageUploadView
和浏览
视图以创建所需的路径
- Point the CKEditor Uploader URLs to your version of the views
- Update the CKEditor Uploader widget through the corresponding form views
- Override the CKEditor Uploader
ImageUploadView
andbrowse
views to create the path you want
示例
更新的CKEditor URL路径
Examples
Updated CKEditor URL Paths
path('myapp/<int:org_id>/report/<int:report_id>/finding/<int:finding_id>/image/upload', never_cache(ck_views.upload),
path('myapp/<int:org_id>/report/<int:report_id>/finding/<int:finding_id>/images', never_cache(ck_views.browse), name='ckeditor_browse'),
小部件更新
Widget Update
def get(self, request, *args, **kwargs):
context = {}
obj = self.get_object()
if obj is not None:
context['org'] = obj.report.org.id
form = FindingForm(instance=obj)
# Set image browse/upload path
image_kwargs = {
'finding_id': obj.id,
'org_id': obj.report.org.id,
'report_id': obj.report.id,
}
image_browse_path = reverse('ckeditor_browse', kwargs=image_kwargs)
image_upload_path = reverse('ckeditor_upload', kwargs=image_kwargs)
form.fields['description'].widget.config['filebrowserBrowseUrl'] = image_browse_path
form.fields['description'].widget.config['filebrowserUploadUrl'] = image_upload_path
context['form'] = form
return render(request, self.template_name, context)
这篇关于ckeditor_uploader动态图像上传路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!