Flask requests.args keep variable in new app route











up vote
1
down vote

favorite












i am getting a variable using request.args. I would like to add another app route to obtain a new variable that will link to the client_id in the app route '/search/'. how do i keep this client_id variable when adding another app route?



from flask import Flask
from flask import render_template, url_for, request, redirect
from flask import make_response
app = Flask(__name__)
import pandas as pd


@app.route('/', methods=['GET'])
def index():
res = '''<form action="/search" method=>
<p><input type="text" name="query" value="test"></p>
<p><input type="submit" value="Search"></p>
<br />
</form>'''
return res

@app.route('/search', methods=["GET","POST"])
def suggestion():
client_id = request.args['query']
return render_template('untitled2.html')


if __name__ == '__main__':
app.run(debug=True)


for example, I want to add this app route



@app.route('/csv/')
def download_csv():
#can't figure out how to obtain the client_id var
client_id = ???
model_id=request.args['textid']
model_id1=request.args['textid1']
model_id2=request.args['textid2']

# return response
df=recommender.update_history(client_id, model_id, model_id1, model_id2)
df= recommender.get_csv()
resp = make_response(df.to_csv(encoding='iso-8859-1',index=False))
resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
resp.headers["Content-Type"] = "text/csv"
return resp


here's what I have on my html template where i need to grab the variable that was formerly in request.args['text'] and send it to the new app route "/csv" along with the new form data:



untitled2.html:



 <div class="form-group">
<h4>Suggestions for {{ request.args['text'] }} </h4>
<form action ="/csv">
<form method="POST">
<br>
<label>Enter 3 suggestions</label>
<br>
<input name="textid", placeholder="Suggested Model ID #", value="{{ request.form['textid'] }}"/>
<input name="textid1", placeholder="Suggested Model ID #", value="{{ request.form['textid1'] }}"/>
<input name="textid2", placeholder="Suggested Model ID #", value="{{ request.form['textid2'] }}"/>

<input type="submit" >
</form>
</div>

<h4>Entered Suggestions:{{ request.form['textid'] }} {{ request.form['textid1'] }} {{ request.form['textid2'] }} </h4>









share|improve this question






















  • In html file, you are using request.args['text'], but in app.py you use client_id = request.args['query']... mistake ?
    – Dinko Pehar
    Nov 9 at 4:17















up vote
1
down vote

favorite












i am getting a variable using request.args. I would like to add another app route to obtain a new variable that will link to the client_id in the app route '/search/'. how do i keep this client_id variable when adding another app route?



from flask import Flask
from flask import render_template, url_for, request, redirect
from flask import make_response
app = Flask(__name__)
import pandas as pd


@app.route('/', methods=['GET'])
def index():
res = '''<form action="/search" method=>
<p><input type="text" name="query" value="test"></p>
<p><input type="submit" value="Search"></p>
<br />
</form>'''
return res

@app.route('/search', methods=["GET","POST"])
def suggestion():
client_id = request.args['query']
return render_template('untitled2.html')


if __name__ == '__main__':
app.run(debug=True)


for example, I want to add this app route



@app.route('/csv/')
def download_csv():
#can't figure out how to obtain the client_id var
client_id = ???
model_id=request.args['textid']
model_id1=request.args['textid1']
model_id2=request.args['textid2']

# return response
df=recommender.update_history(client_id, model_id, model_id1, model_id2)
df= recommender.get_csv()
resp = make_response(df.to_csv(encoding='iso-8859-1',index=False))
resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
resp.headers["Content-Type"] = "text/csv"
return resp


here's what I have on my html template where i need to grab the variable that was formerly in request.args['text'] and send it to the new app route "/csv" along with the new form data:



untitled2.html:



 <div class="form-group">
<h4>Suggestions for {{ request.args['text'] }} </h4>
<form action ="/csv">
<form method="POST">
<br>
<label>Enter 3 suggestions</label>
<br>
<input name="textid", placeholder="Suggested Model ID #", value="{{ request.form['textid'] }}"/>
<input name="textid1", placeholder="Suggested Model ID #", value="{{ request.form['textid1'] }}"/>
<input name="textid2", placeholder="Suggested Model ID #", value="{{ request.form['textid2'] }}"/>

<input type="submit" >
</form>
</div>

<h4>Entered Suggestions:{{ request.form['textid'] }} {{ request.form['textid1'] }} {{ request.form['textid2'] }} </h4>









share|improve this question






















  • In html file, you are using request.args['text'], but in app.py you use client_id = request.args['query']... mistake ?
    – Dinko Pehar
    Nov 9 at 4:17













up vote
1
down vote

favorite









up vote
1
down vote

favorite











i am getting a variable using request.args. I would like to add another app route to obtain a new variable that will link to the client_id in the app route '/search/'. how do i keep this client_id variable when adding another app route?



from flask import Flask
from flask import render_template, url_for, request, redirect
from flask import make_response
app = Flask(__name__)
import pandas as pd


@app.route('/', methods=['GET'])
def index():
res = '''<form action="/search" method=>
<p><input type="text" name="query" value="test"></p>
<p><input type="submit" value="Search"></p>
<br />
</form>'''
return res

@app.route('/search', methods=["GET","POST"])
def suggestion():
client_id = request.args['query']
return render_template('untitled2.html')


if __name__ == '__main__':
app.run(debug=True)


for example, I want to add this app route



@app.route('/csv/')
def download_csv():
#can't figure out how to obtain the client_id var
client_id = ???
model_id=request.args['textid']
model_id1=request.args['textid1']
model_id2=request.args['textid2']

# return response
df=recommender.update_history(client_id, model_id, model_id1, model_id2)
df= recommender.get_csv()
resp = make_response(df.to_csv(encoding='iso-8859-1',index=False))
resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
resp.headers["Content-Type"] = "text/csv"
return resp


here's what I have on my html template where i need to grab the variable that was formerly in request.args['text'] and send it to the new app route "/csv" along with the new form data:



untitled2.html:



 <div class="form-group">
<h4>Suggestions for {{ request.args['text'] }} </h4>
<form action ="/csv">
<form method="POST">
<br>
<label>Enter 3 suggestions</label>
<br>
<input name="textid", placeholder="Suggested Model ID #", value="{{ request.form['textid'] }}"/>
<input name="textid1", placeholder="Suggested Model ID #", value="{{ request.form['textid1'] }}"/>
<input name="textid2", placeholder="Suggested Model ID #", value="{{ request.form['textid2'] }}"/>

<input type="submit" >
</form>
</div>

<h4>Entered Suggestions:{{ request.form['textid'] }} {{ request.form['textid1'] }} {{ request.form['textid2'] }} </h4>









share|improve this question













i am getting a variable using request.args. I would like to add another app route to obtain a new variable that will link to the client_id in the app route '/search/'. how do i keep this client_id variable when adding another app route?



from flask import Flask
from flask import render_template, url_for, request, redirect
from flask import make_response
app = Flask(__name__)
import pandas as pd


@app.route('/', methods=['GET'])
def index():
res = '''<form action="/search" method=>
<p><input type="text" name="query" value="test"></p>
<p><input type="submit" value="Search"></p>
<br />
</form>'''
return res

@app.route('/search', methods=["GET","POST"])
def suggestion():
client_id = request.args['query']
return render_template('untitled2.html')


if __name__ == '__main__':
app.run(debug=True)


for example, I want to add this app route



@app.route('/csv/')
def download_csv():
#can't figure out how to obtain the client_id var
client_id = ???
model_id=request.args['textid']
model_id1=request.args['textid1']
model_id2=request.args['textid2']

# return response
df=recommender.update_history(client_id, model_id, model_id1, model_id2)
df= recommender.get_csv()
resp = make_response(df.to_csv(encoding='iso-8859-1',index=False))
resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
resp.headers["Content-Type"] = "text/csv"
return resp


here's what I have on my html template where i need to grab the variable that was formerly in request.args['text'] and send it to the new app route "/csv" along with the new form data:



untitled2.html:



 <div class="form-group">
<h4>Suggestions for {{ request.args['text'] }} </h4>
<form action ="/csv">
<form method="POST">
<br>
<label>Enter 3 suggestions</label>
<br>
<input name="textid", placeholder="Suggested Model ID #", value="{{ request.form['textid'] }}"/>
<input name="textid1", placeholder="Suggested Model ID #", value="{{ request.form['textid1'] }}"/>
<input name="textid2", placeholder="Suggested Model ID #", value="{{ request.form['textid2'] }}"/>

<input type="submit" >
</form>
</div>

<h4>Entered Suggestions:{{ request.form['textid'] }} {{ request.form['textid1'] }} {{ request.form['textid2'] }} </h4>






flask jinja2 html-form






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 21:02









Julia Dills

758




758












  • In html file, you are using request.args['text'], but in app.py you use client_id = request.args['query']... mistake ?
    – Dinko Pehar
    Nov 9 at 4:17


















  • In html file, you are using request.args['text'], but in app.py you use client_id = request.args['query']... mistake ?
    – Dinko Pehar
    Nov 9 at 4:17
















In html file, you are using request.args['text'], but in app.py you use client_id = request.args['query']... mistake ?
– Dinko Pehar
Nov 9 at 4:17




In html file, you are using request.args['text'], but in app.py you use client_id = request.args['query']... mistake ?
– Dinko Pehar
Nov 9 at 4:17












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You can keep your client_id variable, by adding it to session, save it in cookie or
use gobject.



For session, just use instead of client_id, session['client_id'] = request.args['query']. You can always get it with session['client_id'] . Additionally, you must set secret key in config of app like so app.config['SECRET_KEY'] = 'My secret key'.



Read more about sessions here.






share|improve this answer

















  • 1




    thanks! worked perfectly!
    – Julia Dills
    Nov 9 at 14:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216089%2fflask-requests-args-keep-variable-in-new-app-route%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You can keep your client_id variable, by adding it to session, save it in cookie or
use gobject.



For session, just use instead of client_id, session['client_id'] = request.args['query']. You can always get it with session['client_id'] . Additionally, you must set secret key in config of app like so app.config['SECRET_KEY'] = 'My secret key'.



Read more about sessions here.






share|improve this answer

















  • 1




    thanks! worked perfectly!
    – Julia Dills
    Nov 9 at 14:34















up vote
1
down vote



accepted










You can keep your client_id variable, by adding it to session, save it in cookie or
use gobject.



For session, just use instead of client_id, session['client_id'] = request.args['query']. You can always get it with session['client_id'] . Additionally, you must set secret key in config of app like so app.config['SECRET_KEY'] = 'My secret key'.



Read more about sessions here.






share|improve this answer

















  • 1




    thanks! worked perfectly!
    – Julia Dills
    Nov 9 at 14:34













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You can keep your client_id variable, by adding it to session, save it in cookie or
use gobject.



For session, just use instead of client_id, session['client_id'] = request.args['query']. You can always get it with session['client_id'] . Additionally, you must set secret key in config of app like so app.config['SECRET_KEY'] = 'My secret key'.



Read more about sessions here.






share|improve this answer












You can keep your client_id variable, by adding it to session, save it in cookie or
use gobject.



For session, just use instead of client_id, session['client_id'] = request.args['query']. You can always get it with session['client_id'] . Additionally, you must set secret key in config of app like so app.config['SECRET_KEY'] = 'My secret key'.



Read more about sessions here.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 4:29









Dinko Pehar

7992324




7992324








  • 1




    thanks! worked perfectly!
    – Julia Dills
    Nov 9 at 14:34














  • 1




    thanks! worked perfectly!
    – Julia Dills
    Nov 9 at 14:34








1




1




thanks! worked perfectly!
– Julia Dills
Nov 9 at 14:34




thanks! worked perfectly!
– Julia Dills
Nov 9 at 14:34


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216089%2fflask-requests-args-keep-variable-in-new-app-route%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini