I'm scraping some content and trying to display on html page but it display nothing when i try to run jinja loop for dictionary i sent from python file, it runs every variable separately but not running loop on dictionary
Python dictionary i'm trying to display
dictionary = dict(
[('page_title', url), ('title', title), ('anchor', anchor),
('images', images)])
Html code with jinja tags
<div id="dictionary">
{% for items in dictionary %}
<div style="width: 18rem;">
<img src="{{ items.images }}" alt="{{ items.title }}">
<div >
<a href="{{ items.anchor }}" ><h5 >{{ items.title }}</h5></a>
</div>
</div>
{% endfor %}
CodePudding user response:
No any for loop is needed. Directly, you can use dictionary data
<div style="width: 18rem;">
<img src="{{ dictionary.images }}" alt="{{ dictionary.title }}">
<div >
<a href="{{ dictionary.anchor }}" ><h5 >{{ dictionary.title }}</h5></a>
</div>
</div>
CodePudding user response:
You can create custom template tag like this:
templatetags/tags.py
from django.template.defaulttags import register
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
your_html.html:
<div id="dictionary">
<div style="width: 18rem;">
<img src="{{ dictionary|get_item:'images' }}" alt="{{ dictionary|get_item:'title' }}">
<div >
<a href="{{ dictionary|get_item:'anchor' }}" ><h5 >{{ dictionary|get_item:'title' }}</h5></a>
</div>
</div>
