如何使用Django FileField在模板中显示当前文件 [英] How to show current file in templates with Django FileFields
问题描述
我一直在搜索,似乎无法找到解决方案,这可能是因为它是一个简单的事情。
问题是,我有一个ModelForm,它有一个FileField,当我用这个模型的一个实例渲染这个表单,并且这个对象包含一个上传的文件,我想显示当前的文件,(像管理员那样),但我似乎无法做到这一点。
这些技术都无法工作:
{{form.as_p}}
{{field.label_tag}}
我一直在管理员模板中搜索,但似乎无法找到魔术。表单正确呈现对象的所有其他数据
问题2是因为上传的文件在用对象的实例呈现更改表单时没有显示,那么当我尝试保存时,表单无法验证,因为它对之前上传的文件一无所知。
那么如何处理Django中变化形式的文件字段,如何显示当前上传的文件,以及如何验证表单。 解决方案
这个解决方案是一个在contrib / admin / widgets.py中的自定义小部件,也就是这个:
class AdminFileWidget(forms.FileInput) :
一个FileField Widget,如果有的话显示它的当前值
def __init __(self,attrs = {}):
超级(AdminFileWidget,self).__ init __(attrs)
$ b $ def命令(self,name,value,attrs = None):
output = []
if hasattr(value,url):
output.append('%s< a target =_ blankhref =%s>%s< / a> < br />%s'%\
(_('Currently:'),value.url,value,_('Change:')))
output.append(super AdminFileWidget,self).render(name,value,attrs))
return mark_safe(u''。join(output))
您可以使用这个(或改编版本)使用 据我所知,你的第二个选项是默认字段类型或小部件 =nofollow noreferrer>小部件meta选项
I am kinda stuck, I've been using Django for a while now, but I cant actually seem to find this thing out. And thats weird because it should be a simple thing.
I've been googling around and can't seem to find a solution, it maybe because it is a simple thing.
The problem is, I have a ModelForm, and it has a FileField, when I render this form with an instance of the model, and the object contains an uploaded file, I would like to show the current file, (like the admin does), but I cant seem to do that.
Neither of these techniques work:
{{ form.as_p }}
{{ field.label_tag }}
I've been searching around in the admins templates, but can't seem to find the magic. The form renders all the other data of the object correctly
Problem number 2 is that since the uploaded file doesn't show when rendering a change form with a instance of an object, the forms fail to validate when i try to save since it knows nothing about the file that was previously uploaded.
So how do you handle filefields in a change form in Django, how to show the current uploaded file, and how to validate the form.
You didn't look around well enough, the solution is a custom widget in contrib/admin/widgets.py, namely this one:
class AdminFileWidget(forms.FileInput):
"""
A FileField Widget that shows its current value if it has one.
"""
def __init__(self, attrs={}):
super(AdminFileWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
output = []
if value and hasattr(value, "url"):
output.append('%s <a target="_blank" href="%s">%s</a> <br />%s ' % \
(_('Currently:'), value.url, value, _('Change:')))
output.append(super(AdminFileWidget, self).render(name, value, attrs))
return mark_safe(u''.join(output))
You can use this (or an adapted version) using the widgets meta option of ModelForms.
As far as I know your second problem shouldn't be there at all, even without a nicer widget, let it know if it still persists after using a custom widget.
这篇关于如何使用Django FileField在模板中显示当前文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!