How can I get a parameter passed in the url?


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.

    return render_template('stock.html')

Many thanks in advance.

10/18/2012 6:45:33 PM

Accepted Answer

  1. It's request (imported from the flask package), not self.request

  2. The Flask docs have, as always, thorough documentation about variables in the URL:

    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:

10/18/2012 6:55:22 PM

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow