如何在没有POST请求的情况下获取WTF表单对象数据?

时间:2013-03-04 18:30:45

标签: python flask wtforms flask-wtforms

我有一个模板,允许用户输入搜索参数(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请求获取搜索结果并显示在上面的模板中。在两个搜索字段的keyup上调用ajax函数

$.ajax(
    {
      url: ‘result’,
      dataType: "html",
      data:createQueryString(),// gets the value from the dataFields and creates a queryString 
      success: function(data) 
      {
                              $("# searchResults").html(data);      }    });

我还有2个视图,一个用于searchcriteria部分(包含2个搜索字段的部分),另一个用于搜索结果部分。

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请求,但根据我的设计,没有必要发布请求。

我已尝试将ajax请求作为post请求,但即使这样,wtf表单对象也会返回空白。

现在我唯一的另一个选择是,如果我在进行ajax调用时在查询字符串中传递搜索条件,但不确定这是否是最佳方法。请在此情况下建议如何继续。

1 个答案:

答案 0 :(得分: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)

从您的查看功能上方(路由到&#39;结果&#39;),您的问题是searchform = SearchForm()没有填充提供的GET查询。

要解决此问题,您必须将值提供给searchform。有很多方法可以做到这一点(最明显的方法是通过searchform = SearchForm(formdata = request.args)摆弄表单对象),使用flask扩展flask-wtf可以更容易地完成。

这是一种方法(我将提供我的示例,因为我不知道如何构建查询字符串):

in 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)
  ..
希望有所帮助。