Jinja ничего не отображает через цикл for. Не работает for в Jinja
HTML код:
<!DOCTYPE html>
<div class="row">
{% block content %}
<!-- Latest Products -->
{% for post in posts %}
<div class="col-sm-4 sm-margin-b-50">
<div class="margin-b-20">
<div class="wow zoomIn" data-wow-duration=".3" data-wow-delay=".1s">
<img class="img-responsive" alt="Latest Products Image" src="{{ url_for('static', filename= 'img/970x647/01.jpg') }}">
<!-- <img class="img-responsive" src="img/970x647/01.jpg" alt="Latest Products Image"> -->
</div>
</div>
<h4><a href="#">{{ post['title'] }}</a> <span class="text-uppercase margin-l-20"></span></h4>
<p>Lorem ipsum dolor sit amet consectetur adipiscing elit sed tempor incdidunt ut laboret dolor magna ut consequat siad esqudiat dolor</p>
<a class="link" href="#">Read More</a>
</div>
{% endfor %}
<!-- End Latest Products -->
{% endblock %}
</div>
app.py код:
import sqlite3
from flask import Flask, render_template
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
app = Flask(__name__)
@app.route('/index-test/')
def index_test():
conn = get_db_connection()
posts = conn.execute('SELECT * FROM posts').fetchall()
conn.close()
return render_template('index-test.html')
init_db.py код:
import sqlite3
connection = sqlite3.connect('database.db')
with open('schema.sql') as f:
connection.executescript(f.read())
cur = connection.cursor()
cur.execute("INSERT INTO posts (title, content, description) VALUES (?, ?, ?)",
('First Post', 'Content for the first post', 'Description content for the first post')
)
cur.execute("INSERT INTO posts (title, content, description) VALUES (?, ?, ?)",
('Second Post', 'Content for the second post', 'Description content for the second post')
)
cur.execute("INSERT INTO posts (title, content, description) VALUES (?, ?, ?)",
('Third Post', 'Content for the third post', 'Description content for the third post')
)
connection.commit()
connection.close()
schema.sql код:
DROP TABLE IF EXISTS posts;
CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
description TEXT NOT NULL,
content TEXT NOT NULL
);
Проблема в том что на сайте абсолютно пусто, хотя database.db не пуст. Как будто цикла for вовсе нету в html файле
Ответы (1 шт):
Автор решения: Matthew
→ Ссылка
в app.py должно глядеть так
return render_template('index-test.html', posts=posts)