【问题标题】:How to point FilePond to django media files如何将 FilePond 指向 django 媒体文件
【发布时间】:2020-10-08 10:31:04
【问题描述】:

我正在使用 FilePond.js 来允许用户在网站上拖放图像。我正在尝试设置 FilePond 来检索用户照片输入。显然,我的意思是将照片上传到的媒体链接。像这样:

 <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 媒体网址。

如何将 FilePond 指向我的照片所在的网址 要存储吗?

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

P.S 我正在尝试指向 FilePond 将其上传到我的媒体网址 开发服务器。这是http://www.example.local:8000/media

【问题讨论】:

标签: javascript django filepond


【解决方案1】:

处理/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 脚本,您应该会看到文件对象出现在后端。

【讨论】:

  • 如果我使用这种方法,我还需要在服务器选项中指定一些东西吗?因为我试过了,但我得到了一个空列表。
  • 服务器选项必须指向后端脚本位置
  • 谢谢我想通了。请为可能遇到同样问题的未来用户投票。
【解决方案2】:

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

django-filepond

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

这还处于早期阶段,但也许会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-08-22
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2013-09-03
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多