Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating an input tag with "accept" attribute in Django form using FileField

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.

I've looked at the Django docs at: https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.FileField

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?

like image 671
Steve Nelson Avatar asked Aug 27 '14 21:08

Steve Nelson


1 Answers

Easy. In your model form, just make sure the FileInput widget gets created with the needed accept HTML attribute:

from django import forms

class VideoForm(forms.ModelForm):
    class Meta:
        model = Video
        exclude = []
        widgets = {
            'video_file': forms.FileInput(attrs={'accept': '.mov,video/quicktime'}),
        }
like image 57
Flimm Avatar answered Sep 28 '22 06:09

Flimm