如何在不发POST请求的情况下获取WTF表单对象数据?
我有一个模板,允许用户输入搜索参数(search.html)
{% from "_formhelpers.html" import render_field %}
<form method=”post”>
<dl>
{{ render_field(form. notificationId) }}
{{ render_field(form. recordName) }}
</dl>
<div id="searchResults" > </div>
</form>
宏
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
还有一个用于显示搜索结果的模板(result.html)
{% for notification in content %}
Result:
content[notification]['type'] }}</td>
{% endfor %}
我使用以下的ajax请求来获取搜索结果,并在上面的模板中显示。这个ajax函数是在用户在两个搜索框中输入时触发的
$.ajax(
{
url: ‘result’,
dataType: "html",
data:createQueryString(),// gets the value from the dataFields and creates a queryString
success: function(data)
{
$("# searchResults").html(data); } });
我还有两个视图,一个是搜索条件部分(包含两个搜索框的部分),另一个是搜索结果部分。
class NotificationSearchView(MethodView):
def get(self):
searchform = SearchForm()
return render_template("search.html”, searchform=searchform)
@classmethod
def registerSelf(cls, app):
NotificationSearchView.app = app
app.flaskApp.add_url_rule('/search ', view_func=NotificationSearchView.as_view(‘search’))
class NotificationResultView(MethodView):
def get(self):
searchform = SearchForm()
success, content=self.performSearch(searchform) //search method which takes the search parameters and performs the search
return render_template("result.html”, content=content)
@classmethod
def registerSelf(cls, app):
NotificationResultView.app = app
app.flaskApp.add_url_rule('/result', view_func= NotificationResultView.as_view(‘result’))
WTF表单类
from wtforms import Form, TextField
class SearchForm(BaseForm):
notificationId = TextField(notificationId)
recordName = TextField(recordName)
我遇到的问题是,当我向NotificationResultView发起ajax请求时,wtf表单对象没有被填充。我认为这是因为没有发送post请求,但根据我的设计,其实不需要post请求。
我尝试将ajax请求改为post请求,但即便如此,wtf表单对象仍然是空的。
现在我唯一的其他选择是,在发起ajax请求时将搜索条件放在查询字符串中,但我不确定这是否是最好的方法。请建议我该如何进行。
1 个回答
1
$.ajax(
{
url: ‘result’,
dataType: "html",
success: function(data)
{
$("# searchResults").html(data); } });
在你的ajax函数里,你没有发送任何数据。
更新:好的,看来你实际上是发送了数据,并且网址是正确的。
class NotificationResultView(MethodView):
def get(self):
searchform = SearchForm()
success, content=self.performSearch(searchform) //search method which takes the search parameters and performs the search
return render_template("result.html”, content=content)
根据你上面的视图函数(它被路由到'result'),你的问题是searchform = SearchForm()
没有填充你提供的GET查询参数。
要解决这个问题,你需要把值提供给searchform
。有很多方法可以做到这一点(最明显的就是在创建表单对象时调整一下,比如searchform = SearchForm(formdata = request.args)
),而且使用flask扩展flask-wtf
可以更简单地做到这一点。
这里有一种方法可以做到(我会提供我的例子,因为我不知道你是如何构建你的查询字符串的):
在html中:
query_obj = { foo : 'bar', bar : 'baz', answer : 42 };
$.params(query_obj); //foo=bar&bar=baz&answer=42
/* ajax using above params */
假设参数成功地传递给了视图:
@app.route('/result', method=['GET'])
def fooview():
foo = request.args.get('foo', '')
bar = request.args.get('bar', '')
answer = request.args.get('answer', None, type=int)
..
希望这能帮到你。