简体   繁体   中英

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. I also need to change the inner HTML of my div and hence trying to pass both the innerHTML and form together as below. I get a error that forms are not JSON serializable.

Any suggestions on how to do this on a AJAX call, rather than render a new page ?

Page is an object from the class described below.

@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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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