0

I have a python dictionary which stores the HTML title and comments from the user. I want to display each comment on a new line. I'm wondering is there a way to do this through some sort of loop like a while or for? I am displaying it manually for now. I am using flask if that helps. Here is my code:

Python:

with open ("comments.txt", "r") as myfile:
            data=myfile.read()
display_information = {
            'title':'My title'
            'messages':data.split('*')
}
    return render_template('test.html', pageinfo=display_information)

HTML:

{{ pageinfo.messages[0] }}
<br>        
{{ pageinfo.messages[1] }}
<br>
{{ pageinfo.messages[2] }}

Is there a fast way of displaying the messages on a new line?

1 Answer 1

2

Iterate pageinfo.messages:

{% for msg in pageinfo.messages %}
    {{ msg }}<br>
{% endfor %}
Sign up to request clarification or add additional context in comments.

5 Comments

Cool i'll accept this answer in a minute. Would you know of any way in which if the user wanted to edit the comment, it would then be updated in the text file too? Struggling to find some info.
@fathertodumptious, I don't get your question in the comment. Could you post it as a separate question?
Yeah sure, i'll post it soon.
It seems I can only post every 90 minutes so I will leave another comment here when it is posted.
@fathertodumptious, You don't need to post here. There are many people who can answer your question. I'm not the only one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.