我正在尝试从SQL Server获取客户列表,并将其作为下拉列表显示在网页中。该页面运行正常(显示标题),但没有显示客户列表,也没有错误。
我通过在提取后打印它来测试SQL结果,并且工作正常。还将结果作为段落显示在网页中,并且所有客户名称再次显示在段落中。唯一缺少的是如何在下拉列表中包含客户名称和ID(或在下拉列表中仅包含客户名称)。
这是我的代码和相关的html:
@app.route('/about')
def about():
cnxn = pyodbc.connect("Driver={SQL
server};server=xxx;database="";uid="";pwd="";")
result_df=pd.read_sql("select distinct Customer_Name,Customer_Id from
table_x where period='Q3 2018'",cnxn)
cnxn.close()
print (result_df) # The print here worked fine , I could see all cust
return render_template('customer.html',result_df=result_df)
Customer.html:
{% block body %}
<h1>Sample ECL customer list</h1>
<p>{{result_df}}</p> -- this line works fine display all the customer id and name as a paragraph.
<div class="large-8 columns">
<div class="row container1">
{% for result_df in result_df %}
<div class="large-4 small-4 columns">
<ul>
{% if result_df.Customer_Name %}<li>{{ result_df.Customer_Name }}</li>{% endif %}
<!-- {% if result_df.Customer_Id %}<li
class="price">${{ result_df.customer_id }}</li>{% endif %} -->
</ul>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
答案 0 :(得分:0)
因此,read_sql方法返回数据帧。您可以通过键(Customer_Name,Customer_Id)轻松查看数据
{% for result in result_df %}
<div class="large-4 small-4 columns">
<ul>
{% if result["Customer_Name"] %}<li>{{ result["Customer_Name"] }}</li>{% endif %}
{% if result["Customer_Id"] %}
<li class="price">${{ result["Customer_Id"] }}</li>
{% endif %}
</ul>
</div>
{% endfor %}