2017-10-20 6 views
0

MongoDb에서 결과를 가져 와서 jinja 템플릿으로 전달하는 속기 메소드를 발견했습니다. 플라스크 진자 표시 날짜 필드가 올바르게

@app.route('/home') 
def home(): 
    table = mongo.db.posts 
    result = table.find({ }).sort([("postdate", 1)]) 

    records = json.loads(json_util.dumps(result)) 
    if result.count() > 0: 
     return render_template('users/index.html', posts=records) 
    else: 
     message = 'I couldn't find any post' 
     return render_template('users/index.html', message=message) 

그리고 사용자/index.html을에서이 같은 결과를 표시하고 싶은

: 날짜 필드를 제외하고 예상대로

{% for post in posts %} 
<tr> 
    <td>{{post._id}}</td> 
    <td>{{post.title}}</td> 
    <td>{{post.author}}</td> 
    <td class="date">{{post.postdate}}</td> 
</tr> 
{% endfor %} 

모든 작동합니다. 날짜 필드를 올바르게 표시하는 방법이 있습니까? 배열을 보내기 전에

당신이 읽을 수있는 형식으로 날짜를 변환하는 당신 안에이보기를 사용 CA :

{'$date': 1508227970796} 
{'$date': 1508228089163} 
{'$date': 1508241780398} 

답변

1

은 당신이보고있는 것은이 문제를 해결하기 위해 두 가지 가능성이있다,이 날짜의 소인입니다 신사 템플릿

from datetime import datetime 
datetime.fromtimestamp(the_date_you_want_to_convert) 

하거나 당신이 읽을 수있는 날짜 형식으로 주어진 타임 스탬프를 변환하는 simple Jinja filter which을 만들 수 있습니다.