Show HTML tags in list in Django template

46 views
Skip to first unread message

Moreplavec

unread,
Apr 15, 2017, 4:40:06 PM4/15/17
to Django users
Greetings,

i'm trying to parse HTML website and present same basic info about it. I'm using BeuatifulSoup4 and Python3. Code is simple, but i have problem to force Django template show variables in my list. For example this simple code in template:

{{ tags_hx }}
        <h1>Tags H1 - H7</h1>
        <table class="table-striped table table-hover">
            {% for tag in tags_hx %}
            <tr>
                <td>{% autoescape off %}{{ tag|safe }}{% endautoescape %}</td>
            </tr>
            {% endfor %}
        </table>

 while {{ tags_hx }} prints lists exactly i have:

[<h1 style="text-align: center"><span style="color: rgb(178, 34, 34);">Beginners English Tests 1</span></h1>, <h3>Grammar</h3>, <h3>Beginners ESL</h3>, <h3>For Kids</h3>, <h3>Reading Skills</h3>, <h3>Major Exams</h3>, <h3>Writing &amp; Vocab</h3>]

but i can't get or force Django template to show each tag in table. All i get is:

Tags H1 - H7
[Beginners English Tests 1]
[]
[]
[]
[]
[]
[] 

I know, when i insert html code in template it will be used by browser, but just makes me mad i can't get to work it :) Autoescape or |safe doesn't help. Please, can you help me how to force simple output tags in cycle regardless of the consequences?

Thanks!

ludovic coues

unread,
Apr 16, 2017, 12:16:21 PM4/16/17
to django...@googlegroups.com
I don't understand where the [] symbol in your example come from.
Have you tried to get a detailed view of tags_hx, with {{ tags_hx|pprint }} ?
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/23ba36b8-b550-4eb8-acda-382f0cd7e481%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Ludovic Coues
+33 6 14 87 43 42
Reply all
Reply to author
Forward
0 new messages