简体   繁体   English

如何在 Django 模板中使用 filepond javascript 库?

[英]How can i use filepond javascript library in Django template?

Intro : I have Django web app where users are allowed to create posts.简介:我有 Django web 应用程序,允许用户创建帖子。 Each post has multiple images that are associated with that post.每个帖子都有多个与该帖子相关联的图像。

What I want to do : I want to use Filepond javascript library for remove, add more and previews of selected images.我想做什么:我想使用Filepond javascript 库来删除、添加更多和预览选定的图像。

The issue : The code below is working ok without filepond library but if i try to use filepond library the form is submitting only title input without files.问题:下面的代码在没有 filepond 库的情况下工作正常,但是如果我尝试使用 filepond 库,表单只会提交没有文件的title input

views.py视图.py

class NewsCreateView(CreateView):
    form_class = FileForm
    template_name = 'create.html'
    success_url = '/'

    def form_valid(self, form):
        post = form.save(commit=False)
        post.author = self.request.user
        post.save()
        for f in self.request.FILES.getlist('filepond'):
            FileModel.objects.create(post=post, file=f)
        return super().form_valid(form)

create.html创建.html

<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">
<link href="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css" rel="stylesheet">


<div class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="hpanel" style="margin-top: 10px;">
                <form method="POST" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="form-group">
                        <input type="text" id="id_title" name="title" class="form-control">
                        <input type="file" id="id_file" name="file" multiple="true">                    
                    </div>
                    <button class=" btn btn-success btn-block" type="submit">Submit</button>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.js"></script>
<script src="https://unpkg.com/filepond/dist/filepond.min.js"></script>

<script>
    FilePond.registerPlugin(
        FilePondPluginImagePreview
    );

    FilePond.create(
        document.querySelector('input[type="file"]')
    );
</script>

It's not possible to set a file input value, see: https://pqina.nl/blog/the-trouble-with-editing-and-uploading-files-in-the-browser/无法设置文件输入值,请参阅: https://pqina.nl/blog/the-trouble-with-editing-and-uploading-files-in-the-browser/

So FilePond won't update the existing file input.所以 FilePond 不会更新现有的文件输入。

You either have to upload the file asynchronously by setting the FilePond server property or use the Filepond File Encode plugin to encode the file as base64 data which can be sent to the server as a string.您要么必须通过设置 FilePond server属性来异步上传文件,要么使用 Filepond 文件编码插件将文件编码为 base64 数据,该数据可以作为字符串发送到服务器。

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

相关问题 如何在Django模板过滤器中使用Sekizai javascript块? - How can I use a Sekizai javascript block in a Django template filter? 我如何在 django 模板的 if 语句中使用 django 变量到 javascript 字符串变量 - How I can use a django variable in if statement of django template into a javascript string variable 如何使用django模板变量作为参数来调用javascript函数? - How can I use django template variable as an argument to call javascript function? 如何在 django 模板中使用 javascript - How to use javascript in django template 如何在 Django 模板上传递 Python 列表并在 JavaScript 中使用它? - How do I pass Python list on Django template and use it in JavaScript? 如何在 javascript 中使用 Django 模板标签 - How do I use Django template tags inside javascript Django和一个不错的javascript图表模板。 我应该使用哪个js库? - Django and a nice javascript chart template. Which js library should I use? 如何在 Javascript 文件中使用 Django url 模板 - How to use Django url template in a Javascript file 如何在 javascript 中使用模板标签? Django - How to use template tags in javascript? Django 如何在 django if 语句(模板)中使用 javascript 变量? - How to use javascript variable in django if statement (template)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM