我是django文件处理的新手。该过程进展顺利,没有错误,但上传后没有文件存储。
相反,值[]
存储在上载文档的文档列中。
我只上传了一份文件。
查看: -
class Build_Kb(FormView):
template_name = "template.html"
form_class = Build_form
success_url = '/thanks/'
def form_valid(self, form):
title=form.cleaned_data.get("title")
knowledge=form.cleaned_data.get("knowledge")
document=self.request.FILES.getlist('document')
share_with=form.cleaned_data.get("share_with")
instance = Buildkb.objects.create(title=title,knowledge=knowledge,document=document)
for user in share_with:
instance.share_with.add(user)
instance.save()
messages.success(self.request, "Knowledge saved succesfully")
return redirect("/")
模型
class Buildkb(models.Model):
title=models.CharField(max_length=500,blank=True)
knowledge=models.TextField(blank=False)
document = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)
share_with=models.ManyToManyField(User)