Skip to content

Commit

Permalink
Fix #176 -- Allow overriding the img-tag dimensions attributes (#177)
Browse files Browse the repository at this point in the history

Co-authored-by: Johannes Maron <[email protected]>
  • Loading branch information
atnartur and codingjoe committed Jun 29, 2024
1 parent 96af07e commit 4c2636d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pictures/templates/pictures/picture.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<source type="image/{{ type|lower }}"
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 %}"
sizes="{{ media }}">{% endfor %}
<img src="{{ field_file.url }}" alt="{{ alt }}" width="{{ field_file.width }}" height="{{ field_file.height }}"{% include 'pictures/attrs.html' with attrs=img_attrs %}>
<img{% include 'pictures/attrs.html' with attrs=img_attrs %}>
</picture>
7 changes: 6 additions & 1 deletion pictures/templatetags/pictures.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def picture(field_file, img_alt=None, ratio=None, container=None, **kwargs):
tmpl = loader.get_template("pictures/picture.html")
breakpoints = {}
picture_attrs = {}
img_attrs = {}
img_attrs = {
"src": field_file.url,
"alt": img_alt,
"width": field_file.width,
"height": field_file.height,
}
try:
sources = field_file.aspect_ratios[ratio]
except KeyError as e:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def test_picture__additional_attrs_img(image_upload_file):
assert ' loading="lazy"' in html


@pytest.mark.django_db
def test_picture__additional_attrs_img_size(image_upload_file):
profile = Profile.objects.create(name="Spiderman", picture=image_upload_file)
html = picture(profile.picture, ratio="3/2", img_width=500, img_height=500)
assert ' width="500"' in html
assert ' height="500"' in html


@pytest.mark.django_db
def test_picture__additional_attrs_picture(image_upload_file):
profile = Profile.objects.create(name="Spiderman", picture=image_upload_file)
Expand Down

0 comments on commit 4c2636d

Please sign in to comment.