Python Forum

Full Version: Jinja2 HTML <a> tags not rendering properly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys.
I am using Flask for the web interface
I am having trouble rendering the template properly.
This is my code:
@app.route('/announcements')
def announcements():
    announcements = f_list()
    return flask.render_template('announcements.html', announcements=announcements)
announcements.html:
=================================================
<html>
<head>
<title>Announcements | My Dummy Company</title>
</head>
<body>
<center>
<h1>My Dummy Company</h1>
<h2>Announcements</h2>
</center>
{{ announcements }}
</body>
</html>
=================================================

The command f_list() returns this value:
<a href="/announcements/post?id=1510147646">My first post</a><br><a href="/announcements/id?post=1636152462">My announcement</a><br><a href="/announcements/id?post=2490566582">My third post</a><br><a href="/announcements/id?post=2682700533">Hi</a><br><a href="/announcements/id?post=2980593702">duio fgstbhguoh rtgb;fbhuio trhiojtribh tg</a><br><a href="/announcements/id?post=3732746396">my fourth post</a><br><a href="/announcements/id?post=5164710657">Hello World</a><br><a href="/announcements/id?post=5952756057">my sixth post</a><br><a href="/announcements/id?post=9651076261">my seventh post</a><br>

Everything until here works exactly as expected.
The problem shows up when the actual page is rendered.
Here is the part where the generated <a> tags need to go in:
Error:
</center> &lt;a href="/announcements/post?id=1510147646"&gt;kldfhnibhkduhlbliufhbu;dnoibf&lt;/a&gt;&lt;br&gt;&lt;a href="/announcements/id?post=1636152462"&gt;My announcement&lt;/a&gt;&lt;br&gt;&lt;a href="/announcements/id?post=2490566582"&gt;duoisgfugfdg&lt;/a&gt;&lt;br&gt;&lt;a href="/announcements/id?post=2682700533"&gt;Hi&lt;/a&gt;&lt;br&gt;&lt;a href="/announcements/id?post=2980593702"&gt;duio fgstbhguoh rtgb;fbhuio trhiojtribh tg&lt;/a&gt;&lt;br&gt;&lt;a href="/announcements/id?post=3732746396"&gt;isfh;sgufhioghldfighdf;g&lt;/a&gt;&lt;br&gt;&lt;a href="/announcements/id?post=5164710657"&gt;Hello World&lt;/a&gt;&lt;br&gt;&lt;a href="/announcements/id?post=5952756057"&gt;idulfsgvkdfdbhguhfbndlhufib;ldfb&lt;/a&gt;&lt;br&gt;&lt;a href="/announcements/id?post=9651076261"&gt;ildgbkldjkbnldf hgibtgf&lt;/a&gt;&lt;br&gt; </body>
Why do they render in this manner?
Aren't they supposed to be rendered like normal <a> tags?
Is it that I have the generated HTML snippet in one line?
Please help me understand what went wrong.
Thanks in advance Smile
Why are you generating HTML to pass to your template in the first place? Why aren't you passing a list of values and generating the HTML in the template? That's how you're supposed to do it. Jinja is escaping the HTML basically because it doesn't expect you to be passing HTML..
Thanks!
So you are telling that it's better to generate the HTML using Jinja2 and pass the important parameters.
This was the f_list() function:
def f_list():
    u_list = a_list()
    if u_list[0] == '':
        return 'No announcements found.'
    else:
        output = ''
        for post_id in u_list:
            post_details = get(post_id)
            if output == '':
                output = '<a href="/announcements/post?id='+post_id+'">'+post_details['title']+'</a><br>'
            else:
                output = output+'<a href="/announcements/id?post='+post_id+'">'+post_details['title']+'</a><br>'
        return output
So I should use Jinja2's for loop to generate the HTML right?
(Jun-28-2020, 06:06 PM)ChaitanyaPy Wrote: [ -> ]So I should use Jinja2's for loop to generate the HTML right?

Yes.
Thank you @ndc85430