我正在尝试使用Flask构建以下页面并出现以下错误:
NameError: name 'DateField' is not defined
下面是python脚本
import pandas as pd
import psycopg2
from psycopg2 import sql
from io import BytesIO
from datetime import datetime, date
import warnings
import os
from io import BytesIO
def demo_func(name,start_date,end_date):
db_connection details
dwh_cursor = conn.cursor() # set DB Cursor
## The Query
dwh_cursor.execute(sql.SQL(
"""SELECT count(bill_id) from sales where name = {} and created_at between {} and {}""").format(
sql.Literal(name), sql.Literal(start_date), sql.Literal(end_date)))
下面是view.py代码
@app.route('/page1', methods=['GET','POST'])
def carrier_scac():
form = page1Form()
if form.validate_on_submit():
name = form.name.data
start_date = DateField('Start Date', format="%Y-%m-%d",description='This is the start date of the analysis')
end_date = DateField('End Date', format="%Y-%m-%d",description='This is the end date of the Analysis')
output = demo_func(name,start_date,end_date)
return send_file(output, attachment_filename=name + ' ' + ' Details.xlsx',as_attachment=True)
return render_template('page1.html', form=form)
page1.html的代码
<form action=""
enctype="multipart/form-data"
method="POST">
{{form.hidden_tag()}}
<table>
{{ render_field(form.name, size=50) }}
{{ render_field(form.start_date, size=20) }}
{{ render_field(form.end_date, size=20) }}
{% for subfield in form.tracking_info_field %}
{% endfor %}
<tr class="submit">
<td></td>
<td><button type="submit">Get the Report!</button>
在运行page1.html时出现错误
builtins.NameError
NameError: name 'DateField' is not defined
任何人都可以指导我上述代码在哪里出问题。谢谢
答案 0 :(得分:0)
抱歉,我在views.py中输入了错误的列。纠正后解决了这个问题。