如何从本地文本文件中获取内容以在html模板(Python / Flask)中的各行上打印

时间:2016-12-05 18:08:41

标签: python html flask

screenshot of the output我希望从用户那里获得帖子,以便在各行发布。目前,我收到一个名称,电子邮件和评论,将其传递给app.py文件并将其存储到文本文件中。我返回一个姓名,电子邮件,评论和评论的时间。当我读取文件并传回html模板时,帖子会一个接一个地显示(请参阅屏幕截图),并且我试图让它们在彼此之下显示一个。 f.write(“\ n”)导致实际文本文件跳过一行,但模板中不会出现这种情况。

form_action.html

<html>
<div align = "center">

<body style="background-color: #3DC247;">

<head>
<title>Conor McGregor</title>
</head>
<body>

<div align = "center">
<h1 style="font-family:verdana;">I AM EL CHAPO</h1>
<div align = "center">
<h2 style="font-family:verdana;">YOU'LL DO NUTIN.</h2>

<div align = "center">
<h2 style="font-family:verdana;">Disscusion Page</h2>


 <body>
     <div id="container">
         <div class="title">
            <h3 style="font-family:verdana;">Please fill in your details    
           below and your comment to join the discussion</h3>
        </div>
        <div id="content">
            <form method="post" action="{{ url_for('hello') }}">
              <label for="yourname" style="font-family:verdana;">Please 
           enter your name:</label>
              <input type="text" name="yourname" /><br /><br>

              <label for="youremail" style="font-family:verdana;">Please 
           enter your email:</label>
              <input type="text" name="youremail" /><br /><br>

              <label for="yourcomment"style="font-family:verdana;">Please 
           enter your comment:</label>
              <input type="textarea" name="yourcomment" rows="4" cols="50">               
              <input type="submit" /><br>
            </form>


        </div>  
      </div>
     </div>
    </div>
      <div id="container">
         <div class="title">
            <h1 style="font-family:verdana;"><p>Comments</p></h1>
        </div>
        <div id="content">

                    {{details}}

        </div>  

      </div>
    </body>
  </html>

app.py

 from flask import Flask, render_template, request, url_for
 import time
 import datetime


 # Initialize the Flask application
 app = Flask(__name__)

 # Define a route for the default URL, which loads the form
 @app.route('/')
 def form():
    return render_template('form_submit.html')

 @app.route('/hello/', methods=['POST','GET'])
 def hello():
   global time

   name=request.form['yourname']
   email=request.form['youremail']
   comment=request.form['yourcomment']
   comment_time=time.strftime("%a-%d-%m-%Y %H:%M:%S")

   f = open ("user+comments.txt","a")

   f.write(name + '  ' + email + '   ' + comment + "  " + comment_time)
   f.write('\n')
   f.close()

    with open("user+comments.txt", "r") as f:

    details = f.read()
    f.close()

    return render_template('form_action.html', details = details, name=name,   
    email=email, comment=comment, comment_time=comment_time)


    if __name__ == '__main__':
    app.run(debug=True)

1 个答案:

答案 0 :(得分:0)

HTML不知道\n是什么。您可以通过两种方式解决此问题。

  1. str详细信息转换为list详细信息

    details = f.read().split('\n')
    

    这会将str对象转换为list对象。您可以使用

    在模板中打印它
    {% for detail in details %}
         {{detail}}
    {% endfor %}
    
  2. \n替换为<br>

    details = f.read().replace('\n','<br>')
    

    然后在{{ details|safe }}中将其打印为form_action.html。 使用safe filter非常重要。 <br>将在没有它的情况下进行转义,并呈现为简单文字。