我有一个样品瓶应用程序,我使用各种来源的WTForms开发。我的主要目的是首先查看所有选项是否正常工作,以便我开发应用程序,以便再次显示所选选项。
该应用程序是公开可用的here。但是,该应用程序无法正常工作。任何人都可以帮我解决这个问题吗?这是我的routes.py文件,如下所示:
cat
我的html文件如下:
# Import the actual Flask app
from app import app
from flask import render_template, redirect, jsonify, request, url_for
import sys
import os
from forms import PlagSelection
@app.route('/')# methods=['GET', 'POST'])
def select_doc():
form = PlagSelection()
# If file has been selected, perform detection and display results
if form.validate_on_submit():
kwargs = {
form.features.name : form.features.data,
form.cluster_method.name : form.cluster_method.data,
form.atom.name : form.atom.data,
form.similarity_measure.name : form.similarity_measure.data,
form.k.name : form.k.data
}
print kwargs
return redirect(url_for('view_doc', **kwargs))
elif len(form.errors) > 0:
# TODO Make a nice error msg
return str(form.errors)
else:
# Otherwise display options
return render_template('select_doc.html',
form = form)
@app.route('/view_doc/',methods=['GET'])
def view_doc():
atom_type = request.args.get('atom')
features = request.args.getlist('features')
cluster_method = request.args.get('cluster_method')
similarity_measure = request.args.get('similarity_measure')
k = int(request.args.get('k'))
return render_template('view_doc.html',
cluster_method = cluster_method,
k = k,
features = features,
atom_type = atom_type,
similarity_measure = similarity_measure
)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(debug = True, port=port)
**编辑**
我按如下方式更改了routs.py:
<html>
{% block content %}
<body>
<div class="container">
<h1>Pick your plag</h1>
<div class="jumbotron">
<dt>
<form method="POST" action='/view_doc' enctype="multipart/form-data" role="form">
<div class="form-group">
<dd> {{ form.features.label }} </dd>
<dd> {{ form.features(class='form-control') }} </dd>
<dd> {{ form.atom.label }} </dd>
<dd> {{ form.atom(class='form-control') }} </dd>
<dd> {{ form.cluster_method.label }} </dd>
<dd> {{ form.cluster_method(class='form-control') }} </dd>
<dd> {{ form.k.label }} </dd>
<dd> {{ form.k(class='form-control') }} </dd>
<dd> {{ form.similarity_measure.label }} </dd>
<dd> {{ form.similarity_measure(class='form-control') }} </dd>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Go </button>
</div>
</form>
</dt>
</div>
</div>
</body>
{% endblock content %}
</html>
我得到如下错误:
# Import the actual Flask app
from app import app
from flask import render_template, redirect, jsonify, request, url_for, session
import sys
import os
from forms import PlagSelection
@app.route('/')# methods=['GET', 'POST'])
def select_doc():
form = PlagSelection()
# If file has been selected, perform detection and display results
if form.validate_on_submit():
session['form_data']= {
form.features.name : form.features.data,
form.cluster_method.name : form.cluster_method.data,
form.atom.name : form.atom.data,
form.similarity_measure.name : form.similarity_measure.data,
form.k.name : form.k.data
}
return redirect(url_for('view_doc'))
elif len(form.errors) > 0:
# TODO Make a nice error msg
return str(form.errors)
else:
# Otherwise display options
return render_template('select_doc.html',
form = form)
@app.route('/view_doc',methods=['POST'])
def view_doc():
atom_type = session.get('atom')
features = session.get('features')
cluster_method = session.get('cluster_method')
similarity_measure = session.get('similarity_measure')
k = int(session.get('k'))
return render_template('view_doc.html',
cluster_method = cluster_method,
k = k,
features = features,
atom_type = atom_type,
similarity_measure = similarity_measure
)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(debug = True, port=port)
答案 0 :(得分:1)
仍然不是一个MCVE,但我注意到两件事。
尝试将'/view_doc/'
更改为'/view_doc'
。除非您有特殊原因使用尾部斜杠。
另一件事是return redirect(url_for('view_doc', **kwargs))
。我认为这就是&POST;请求&#39;部分错误来自。基于错误,似乎以这种方式传递数据是不安全的。
尝试使用session
来保留表单数据并删除**kwargs
的使用。
session['my_form_data'] = {
form.features.name : form.features.data,
form.cluster_method.name : form.cluster_method.data,
form.atom.name : form.atom.data,
form.similarity_measure.name : form.similarity_measure.data,
form.k.name : form.k.data
}
return redirect(url_for('view_doc'))
在'/view_doc'
中,您可以访问表单数据session.get('atom')
等