如何将 FilePond 指向 django 媒体文件

How to point FilePond to django media files

我正在使用 FilePond.js 来允许用户在网站上拖放图像。我正在尝试设置 FilePond 以检索用户照片输入。显然我打算将照片上传到的媒体 link 中。像这样:

 <script>
    FilePond.registerPlugin(FilePondPluginImagePreview);
    const inputElement = document.querySelector('input[type="file"]');
    const pond = FilePond.create(inputElement);

    FilePond.setOptions({
      server: "#" *** I have tried everthing possible as a server option, nothing works***
    });
</script>


def add(request):
if request.method == "POST":
    product_title = request.POST.get("product_title")
    product_price = request.POST.get("product_price")
    product_description = request.POST.get("product_description")
    product_images = request.FILES.getlist('filepond')
    request.user.product_set.create(product_title = product_title,product_price =product_price, product_description = product_description, product_images = product_images)
return render(request,"main/add.html")


class product(models.Model):
  user = models.ForeignKey(User, on_delete=models.CASCADE)
  product_title =  models.CharField(max_length=100, blank=True)
  product_price =  models.CharField(max_length=30, blank=True)
  product_description =  models.CharField(max_length=1000, blank=True)
  product_images = models.FileField()

但我似乎无法让 FilePond 上传 图像到 Django 媒体 url。

How can I point FilePond to upload to the url where my photo is meant to be stored?

我知道如何从用户输入中收集图像的唯一方法是使用获取请求。我还可以使用获取请求来检索用户提交的 FilePond 图像 并以这种方式将它们上传到数据库吗?

P.S I am trying to point FilePond to upload it to the media url on my development server. Which is http://www.example.local:8000/media

后端脚本处理 /media 应该接受来自名为 filepond 的字段的 POST 请求。见 FilePond docs on how to configure the field name.

查看 Django docs on file uploads 我想这就是你在 Django 中检索文件的方式:

request.FILES['filepond']

如果字段设置为multiple

files = request.FILES.getlist('filepond')

因此,如果您相应地调整 Django 脚本,您应该会看到文件对象出现在后端。

我为 django 写了一个小助手应用程序:

django-filepond

它允许您通过上传路径指定上传的应用程序,您可以编写自定义上传处理程序,然后将上传的文件与正确的模型相匹配。

它处于早期阶段,但也许有帮助。