简体   繁体   English

Flask WTForm作为对AJAX调用的响应

[英]Flask WTForm as response to a AJAX call

In a Flask application, I am trying to "render" different Flask-WTForms based on various button choices. 在Flask应用程序中,我试图根据各种按钮选择来“渲染”不同的Flask-WTForms。 I also need to change the inner HTML of my div and hence trying to pass both the innerHTML and form together as below. 我还需要更改div的内部HTML,因此尝试按以下方式同时传递innerHTML和表单。 I get a error that forms are not JSON serializable. 我收到表单无法JSON可序列化的错误。

Any suggestions on how to do this on a AJAX call, rather than render a new page ? 关于如何通过AJAX调用而不是呈现新页面的任何建议?

Page is an object from the class described below. Page是来自下面描述的类的对象。

@app.route('/_select_forms',methods=['GET','POST'])
def _creation_frame():
    content_requested=request.get_json()
    Page=Page_contribute_frame(content_requested)
    return jsonify(html=Page.html_string, form=Page.form)
class Page:
    def __init__(self, **kwargs):
        self.html_string = None
        self.form = None
<div id=contribute-frame></div>
function contribute_frame(e){
  var request_json = frame_identifier();
  $.ajax({
    type:'POST',
    url:'/_contribute_frame',
    contentType: "application/json",
    data:request_json,
    success : function(data){
      $('#contribute-frame').html(data.html);
    },
    error : function (){
      $('#contribute-frame').html('Browser did not get a response. Check connection.');
    }
  });
}````


通过呈现字符串在Flask中解决了此问题。

return render_template_string(Page.html_string, form=Page.form)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM