使用“接受”生成输入标签属性在Django窗体中使用FileField [英] Generating an input tag with "accept" attribute in Django form using FileField
问题描述
我想创建一个表单来上传和保存iOS Safari中的视频,它支持输入标签的accept属性,例如:
I want to create a form to upload and save videos from iOS Safari, which supports the "accept" attribute of the input tag, for example:
<input type=file accept="video/*">
可让您拍摄和上传新视频或在设备上选择一个视频文件。
lets you shoot and upload new video or select a video file on the device.
我已经看过Django文档: https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.FileField
I've looked at the Django docs at: https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.FileField
但是看不到我可以在哪里指定输入中的accept属性。这可能吗?我可以在模板中创建输入标签,还是使用Django表单处理来接受文件?
but don't see where I can specify the "accept" attribute on input. Is this possible? Can I create the input tag in the template and still use Django form processing to accept the file?
推荐答案
没有简单的方法为此,您可以在这个问题中看到一些很好的答案
此外,个人建议(未经测试):创建表单后,设置您想要的任何属性,如
There is no easy way to do this, you can see some good answers in this question Also, a personal suggestion (untested): after creating the form, set whatever attributes you want on it like this
for f in form.fields:
if isinstance(form.fields[f].widget, ClearableFileInput):
form.fields[f].widget.attrs['accept'] = 'video/*'
这篇关于使用“接受”生成输入标签属性在Django窗体中使用FileField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!