gpt4 book ai didi

javascript - 如何将 FilePond 指向 django 媒体文件

转载 作者:行者123 更新时间:2023-12-03 00:11:26 24 4
gpt4 key购买 nike

我正在使用 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 媒体 URL。

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

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

P.S I am trying to point FilePond to upload it to the media url on mydevelopment 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']

如果字段设置为多个

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

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

关于javascript - 如何将 FilePond 指向 django 媒体文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54708393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com