[Python] Preserve newlines in file rendered to HTML with Jinja

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to print the content of a file in a web page. I want to print each line from the file on a separate line, but instead the line breaks are missing. How do I print the file and preserve newlines?

    @app.route('/users')
    def print_users():
    v = open("users.txt","r").read().strip()
    # also tried:
    # v = open("users.txt","r").read().strip().split('\n')
    return render_template('web.html', v=v)


    {{ v|safe}}

    Continue reading...

Compartilhe esta Página