Binding Files to Forms in Django

限于喜欢 提交于 2021-01-29 03:57:59

问题


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?


回答1:


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

Like:

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


来源:https://stackoverflow.com/questions/1814215/binding-files-to-forms-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!