Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ImageFormField render with accept="image/*"' HTML attribute #731

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sorl/thumbnail/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db.models import Q
from django import forms
from django.utils.translation import gettext_lazy as _
from django.forms.widgets import FileInput

from sorl.thumbnail import default

Expand Down Expand Up @@ -68,3 +69,9 @@ def to_python(self, data):
f.seek(0)

return f

def widget_attrs(self, widget):
attrs = super().widget_attrs(widget)
if isinstance(widget, FileInput) and 'accept' not in widget.attrs:
attrs.setdefault('accept', 'image/*')
return attrs
18 changes: 18 additions & 0 deletions tests/thumbnail_tests/test_formfields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django import forms
from django.test import SimpleTestCase
from django.forms.widgets import FileInput

from sorl.thumbnail.fields import ImageFormField


class ImageFormFieldTest(SimpleTestCase):

def assertWidgetRendersTo(self, field, to):
class Form(forms.Form):
f = field
self.assertHTMLEqual(str(Form()["f"]), to)

def test_widget_attrs_default_accept(self):
f = ImageFormField()
self.assertEqual(f.widget_attrs(FileInput()), {'accept': 'image/*'})
self.assertWidgetRendersTo(f, '<input type="file" name="f" accept="image/*" required id="id_f" />')