T
T
Tujh Tujhov2019-04-21 13:29:39
Python
Tujh Tujhov, 2019-04-21 13:29:39

How to display in html a list passed from python?

There is a function that calls render_template of a template with three parameters

def docs():
    page_title = 'Используемая документация'
    content_title = 'Используемая документация'
    content_text = [
        "https://getmdl.io/components/  -   Документация по Material Design by Google",
        "http://flask.pocoo.org/docs/1.0/   -   Документация по Flask"
    ]
    content_text = "<br/>".join([str(elem) for elem in content_text])
    return render_template('template.html', title=page_title, pagename=content_title, content=content_text)

I accept in the template like this:
{% extends 'base.html' %}
{% block title %}
    {{ title }}
{% endblock %}

{% block content_title %}
    {{ pagename }}
{% endblock %}

{% block content %}
    {{ content }}
{% endblock %}

I want my html list to be displayed on more than one line
https://getmdl.io/components/ - Material Design documentation by Google (no line break) flask.pocoo.org/docs/1.0 - Flask documentation
And in several lines
https://getmdl.io/components/ - Documentation on Material Design by Google (there is a newline)
flask.pocoo.org/docs/1.0 - Documentation on Flask

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kova1ev, 2019-04-22
@kova1ev

you can use cycles in templates, just pass the list to the template, why construct lines in the code

def docs():
    page_title = 'Используемая документация'
    content_title = 'Используемая документация'
    content_text = [
        "https://getmdl.io/components/  -   Документация по Material Design by Google",
        "http://flask.pocoo.org/docs/1.0/   -   Документация по Flask"
    ]
    return render_template('template.html', title=page_title, pagename=content_title, content=content_text)

sample
{% extends 'base.html' %}
{% block title %}
    {{ title }}
{% endblock %}

{% block content_title %}
    {{ pagename }}
{% endblock %}

{% block content %}
    {% for elem in content %}
        {{elem}}<br>
    {% endfor %}
{% endblock %}

It became interesting, I typed your code to find out what does not work. The template engine in the passed strings treats the <> characters exactly as these characters, and not as a tag, that is, it replaces them with < and >

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question