Skip to content

Commit 4c2636d

Browse files
atnarturcodingjoe
andauthored
Fix #176 -- Allow overriding the img-tag dimensions attributes (#177)
Co-authored-by: Johannes Maron <[email protected]>
1 parent 96af07e commit 4c2636d

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

pictures/templates/pictures/picture.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
<source type="image/{{ type|lower }}"
33
srcset="{% for width, pic in srcset.items %}{% if use_placeholders %}{% url 'pictures:placeholder' alt|urlencode:'' ratio width type %}{% else %}{{ pic.url }}{% endif %} {{ width }}w{% if not forloop.last %}, {% endif %}{% endfor %}"
44
sizes="{{ media }}">{% endfor %}
5-
<img src="{{ field_file.url }}" alt="{{ alt }}" width="{{ field_file.width }}" height="{{ field_file.height }}"{% include 'pictures/attrs.html' with attrs=img_attrs %}>
5+
<img{% include 'pictures/attrs.html' with attrs=img_attrs %}>
66
</picture>

pictures/templatetags/pictures.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ def picture(field_file, img_alt=None, ratio=None, container=None, **kwargs):
1414
tmpl = loader.get_template("pictures/picture.html")
1515
breakpoints = {}
1616
picture_attrs = {}
17-
img_attrs = {}
17+
img_attrs = {
18+
"src": field_file.url,
19+
"alt": img_alt,
20+
"width": field_file.width,
21+
"height": field_file.height,
22+
}
1823
try:
1924
sources = field_file.aspect_ratios[ratio]
2025
except KeyError as e:

tests/test_templatetags.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ def test_picture__additional_attrs_img(image_upload_file):
6565
assert ' loading="lazy"' in html
6666

6767

68+
@pytest.mark.django_db
69+
def test_picture__additional_attrs_img_size(image_upload_file):
70+
profile = Profile.objects.create(name="Spiderman", picture=image_upload_file)
71+
html = picture(profile.picture, ratio="3/2", img_width=500, img_height=500)
72+
assert ' width="500"' in html
73+
assert ' height="500"' in html
74+
75+
6876
@pytest.mark.django_db
6977
def test_picture__additional_attrs_picture(image_upload_file):
7078
profile = Profile.objects.create(name="Spiderman", picture=image_upload_file)

0 commit comments

Comments
 (0)