Reputation: 3060
I am making a stock application, where, after a user types in a stock such as MSFT
, they are redirected to .../stock?s=MSFT
I have successfully made this part, but now I need Python to grab this current, unique URL so that I can parse the quote (in this case, the MSFT
) out if it and save it as a new variable.
The easiest way I can think of is just to get the current URL, although I cannot seem to find a way to do this; using self.request...
but this returned the error:
NameError: global name 'self' is not defined
The other ideas I came across was to use a form of urllib
as it contains a specific .request_qs()
method, although this requires me to pass the URL as a parameter, which should be unique every time because of the change in stock.
Here is the Python I currently have:
@app.route('/', methods=['GET', 'POST'])
def home_search():
if request.method == 'POST':
st = request.form['s']
return redirect(url_for('stock')+'?s='+st)
return render_template('stk.html')
@app.route('/stock', methods=['GET', 'POST'])
def stock():
#here I need to save the variable 'name' as the current URL. I can parse it later.
url="http://finance.yahoo.com/d/quotes.csv?s="+name"&f=snl1"
text=urllib2.urlopen(url).read()
return render_template('stock.html')
Many thanks in advance.
Upvotes: 1
Views: 3289
Reputation: 8244
It's request
(imported from the flask package), not self.request
The Flask docs have, as always, thorough documentation about variables in the URL: http://flask.pocoo.org/docs/quickstart/#variable-rules
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username
On that occasion, you should read the whole Quickstart from the Flask docs: http://flask.pocoo.org/docs/quickstart/
Upvotes: 8
Reputation: 5936
You haven't posted any info on where the error occurs :) And I can't see where you're trying to access self.requests
.
Looking at other Flask apps, it seems you should just access request
instead.
Take a look at these examples: https://github.com/mitsuhiko/flask/tree/master/examples
Upvotes: 1