2016-12-05 8 views
1

screenshot of the outputユーザーからの投稿を個別の行に投稿することを検討しています。私は名前、電子メール、およびコメントを取る瞬間、app.pyファイルにそれを渡し、それをテキストファイルに保存します。私は、名前、Eメール、コメント、コメントの時間を返す。私がファイルを読んで、HTMLテンプレートに戻ってくると、ポストは次々に表示されます(スクリーンショットを参照)。そして、それらが互いに下に表示されるようにしようとしています。 f.write( "\ n")を実行すると、実際のテキストファイルは行をスキップしますが、これはテンプレートでは発生しません。ローカルテキストファイルの内容をHTMLテンプレートの個々の行に印刷する方法(Python/Flask)

<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) 

答えて

0

form_action.html HTMLは\nが何であるかを知りません。これは2つの方法で修正できます。

  1. これはlistオブジェクトにstrオブジェクトに変換list詳細

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

    str詳細を変換します。あなたは

    {% for detail in details %} 
        {{detail}} 
    {% endfor %} 
    
  2. <br>

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

    \nを交換し、その後form_action.html{{ details|safe }}としてそれを印刷を使用して、テンプレートにそれを印刷できます。 safe filterを使用することが重要です。 <br>はそれなしでエスケープされ、単純なテキストとしてレンダリングされます。

+1

あなたは男のibrahim – Charles

関連する問題