在Django中将文件绑定到表单 [英] Binding Files to Forms in Django

查看:56
本文介绍了在Django中将文件绑定到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个表单,用户可以在其中保存自己的进度.保存文件后,我已经成功地上传了文件,但是由于某些原因,以下代码使上传的文件不受表单的约束,从而使用户重新上传文件:

I'm trying to create a form where users can save their progress. I have successfully managed to upload files when they are saved, but for some reason the following code leaves the file that was uploaded unbound from the form and thus making the user reupload the file:

class ImageForm(forms.ModelForm):
  class Meta:
    model = MyImage

imageform = ImageForm(instance=a_MyImage_instance)

我想我可以手动获取并设置文档,但是这种行为对我来说有点奇怪.有人可以澄清吗?

I suppose I could go some manual getting and setting a la the documentation, but this behavior seems a bit odd to me. Can someone clarify this?

推荐答案

您可以使用

You could use the widget from admin to see which file is currently uploaded.

赞:

from django.contrib.auth.widgets import AdminFileWidget
class ImageForm(forms.ModelForm):
    nameofimagefield = forms.ImageField(widget=AdminFileWidget)
    class Meta:
        model = MyImage

这篇关于在Django中将文件绑定到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆