在django表单上更新forms.FileField [英] update forms.FileField on django forms
问题描述
我有一个带有FileField的模型:
I have a model with a FileField in it:
class DocumentUpload(models.Model):
document_name = models.CharField(max_length=100, blank=True)
document_path = models.FileField(upload_to='uploads')
和使用此模型的表单
class DocumentUploadForm(forms.ModelForm):
class Meta:
model = DocumentUpload
当我使用表单创建新的上传一切正常。
When I use the form to create a new upload everything works fine.
if request.method == 'POST':
form = DocumentUploadForm(request.POST, request.FILES)
if form.is_valid():
form.save()
但是,当我尝试更新/编辑条目时,会更新所有上传的文档以外的所有字段。这只是原始上传的一样。
However when I try and update/edit the entry it updates all the fields apart from the document which is uploaded. This just stays the same as the original upload.
d = get_object_or_404(DocumentUpload, pk=id)
if request.method == 'POST':
form = DocumentUploadForm(data=request.POST, files=request.FILES, instance=d)
if form.is_valid():
u = form.save()
如何在编辑实例时更改上传文件
How do I get it to change the upload file when editing the instance?
谢谢
推荐答案
既然是我的想法,将其发布为答案(仅仅是为了冲动自己的自我和/或评级)...
Since it was my idea, I'll post it up as an answer (just to stroke my own ego and/or rating)...
将以下内容添加到表单的模板中:
Add the following to your form's template:
enctype="multipart/form-data"
随时随地查看一下...
feel free to check it off as an answer...
:)
这篇关于在django表单上更新forms.FileField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!