Wednesday, 15 June 2011

python - pass variable value to template after receiving a POST request every 5 sec -


There is a problem after my request and I get the variable msg my template login.html is still empty and I want to see that the message has been received. '

app.py

  from the flask import flask, render_template, reading, url_for, request app = flask (__name__) @ app. Root ('/ Reak', methods = ['GET', 'POST']) Def (): msg = None if request.method == 'POST': 'Request' value = if __name__ == '__main__': App.run (debug = true)  

login.html

  & lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; & Lt; / Title & gt; & Lt; Meta name = "viewport" content = "width = device-width, initial-scale = 1.0" & gt; & Lt; Link href = "static / bootstrap.min.css" rel = "stylesheet" media = "screen" & gt; & Lt; Script type = "text / javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; Var test = function {$ (function () {$ .ajax ({type: "POST", url: "/ req", data: {'name': 'asdf'},})}}}} Window .setInterval (function () {test ()}, 5000); & lt; / script & gt; & lt; / head> gt; body & gt; {% msg msg} {{message}} # Empty page {% endif%}    

It seems that you are matching template rendering and Ajax.

Your Javascript test function Call your req viewing method in the back-end, which does renders a template, but You are not doing anything with that template.

Your msg variable that you are trying to print in the template, where your # is empty page Note will never be rendered, because there is no msg variable which is provided as none according to that base template. msg is set by posting via Ajax, but this provided template is never displayed.

If your goal is to see only msg on the output page, return your req method to only one POST : @ app.route ('/ req', methods = ['GET', 'post']) Def (): msg = none if request.method == 'POST': if request.values.get ('Name', none) == 'asdf': Return 'got' Return Rider_template ('login.html', msg = msg)

And in your Ajax call, / P>

  var test = function {$ (function () {$ .ajax ({type: "POST", url: "/ req", data: {'name': 'asdf' }, Success: function (data) {// 'string}});}); }  

If you really want to get your template back as you wrote it in question, then you are better off with the form to submit instead of Ajax so that you get a new page:

  @ app.route ('/ req', methods = ['GET', 'POST']) Def req (): msg = None if request.method == 'Post' : If request.values.get ('name', none) == 'asdf': render_template ('login.html', msg = msg) return render_template ('login.html', msg = none)  

and HTML:

  & lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; & Lt; / Title & gt; & Lt; Meta name = "viewport" content = "width = device-width, initial-scale = 1.0" & gt; & Lt; Link href = "static / bootstrap.min.css" rel = "stylesheet" media = "screen" & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form Action = "/ Recycle" method = "Post" & gt; & Lt; Input type = "text" name = "name" & gt; & Lt; Input type = "submit" value = "submit" & gt; & Lt; / Form & gt; {% Msg msg} {{msg}} #empty page {% endif%} & lt; / Div & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

No comments:

Post a Comment