0
我想通過選擇相應的列來將csv文件上傳到Mysql,但問題是,一旦我更改了路由,文件就會關閉。 所以我試圖渲染2個模板在相同的路線:第一個加載文件,第二個選擇列。我只能訪問第一個模板。 我與測試第二形式env.is_submitted(),但即使我沒有在提交打印 「提交」Flask在不同的文件中呈現兩種形式,但只能得到第一種形式
@app.route('/upload', methods=['GET', 'POST'])
def upload():
form = UploadForm()
global columnscsv, salessource
if form.validate_on_submit():
try:
filename = secure_filename(form.csv.data.filename)
file = form.csv.data
if file and allowed_file(filename):
print 'file_path'
salessource = CSVSource(file, delimiter=',')
columnscsv = salessource.fieldnames
print columnscsv
finally:
return render(salessource)
return render_template('upload.html', form=form)
def render(salessource):
env = envForm()
if env.is_submitted():
print "submitted"
return render_template('form.html',columnscsv = columnscsv ,env =env)
upload.html
<html>
<head>
<title>Upload</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" >
{{ form.hidden_tag() }}
{{ form.csv }}
<input type="submit">
</form></body>
</html>
form.html
{% block body %}
<form name = "mapping" method="POST" enctype="multipart/form-data" >
{{ env.hidden_tag() }}
<table>
{% for csv in columnscsv %}
<tr> <td> {{ csv }}</td>
<td><select name = "{{ csv }}" >
<option >year </option>
<option >month</option>
<option >day</option>
<option>reference</option>
<option>designation</option>
</select></td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Submit" name = "submit" >
</form>
{% endblock %}
謝謝,但我仍然沒有得到它如何訪問form.html中的表單 – nourelhouda
你可以有更多關於'訪問'的細節 –
我的意思是當我呈現模板'form.hml'我不能到達表單中的標籤。那麼我發現了另一種解決方案:我在本地上傳文件並重新使用它。謝謝 – nourelhouda