我试图打印html代码,其中也包含变量,在这种情况下,它的数据来自数据库,包含以下代码:
def displayHot():
myCursor = mongo.db.posts.find()
for result_object in myCursor:
print'''
Content-Type: text/html
<div class="card-header">
<h3 %s
</h3>
</div>
<div class="card-block">
<p> %s
</p>
<a class="panel-google-plus-image" href="{{url_for('Images', filename = 'Images/%s')}}"
</a>
</div"
''' % (result_object['uploader'], result_object['description'], result_object['file_name'])
并在此处呈现:
@app.route('/', methods=['POST','GET'])
def home():
if request.method == 'GET':
return render_template('index.html', function = displayHot())
当我运行它时,我得到了我的cmd中的打印数据..所以我的问题是如何打印html代码以便它将作为HTML代码而不仅仅是cmd中的打印命令
答案 0 :(得分:0)
看起来你可以通过在jinja模板中进行循环来获得更好的服务,并且只需向模板发送循环数据。像我这样的事情就是我在想的:
server::player::Connected connectMessage;
connectMessage.set_xloc(stats.m_xPos);
connectMessage.set_yloc(stats.m_yPos);
connectMessage.set_zrot(stats.m_zRot);
// [0] => username
connectMessage.add_userinfo();
connectMessage.set_userinfo(0, stats.m_clientName);
// [1] => userid
connectMessage.add_userinfo();
connectMessage.set_userinfo(1, stats.m_clientId);
// [2] => shipcolor
connectMessage.add_userinfo();
connectMessage.set_userinfo(2, stats.m_shipColor);
// [3] => shipname
connectMessage.add_userinfo();
connectMessage.set_userinfo(3, stats.m_shipName);
@app.route('/', methods=['POST','GET'])
def home():
if request.method == 'GET':
myCursor = mongo.db.posts.find()
return render_template('index.html', cursor = myCursor)
这是简化的,您可能需要做更多工作才能在将<!-- your other code here -->
{% for result_object in cursor %}
<div class="card-header">
<h3> {{result_object['uploader']}}
</h3>
</div>
<div class="card-block">
<p> {{result_object['description']}}
</p>
<a class="panel-google-plus-image" href="{{url_for('Images', filename = '''Images/result_object['file_name']''')}}"
</a>
</div>
{% endfor %}
<!-- the rest of your html -->
发送到模板之前正确设置myCursor
,但我希望能够实现这一目标。