1

我正在制作一个股票应用程序,在用户输入股票MSFT.../stock?s=MSFT例如在这种情况下,将MSFT) 取出并将其保存为新变量。

我能想到的最简单的方法就是获取当前 URL,尽管我似乎找不到这样做的方法;使用self.request...,但这返回了错误:

NameError: global name 'self' is not defined

我遇到的其他想法是使用一种形式,urllib因为它包含一个特定的.request_qs()方法,尽管这需要我将 URL 作为参数传递,由于库存的变化,每次都应该是唯一的。

这是我目前拥有的 Python:

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

提前谢谢了。

4

2 回答 2

8
  1. 它是request(从烧瓶包中导入的),而不是self.request

  2. Flask 文档一如既往地提供有关 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
    

在那种情况下,您应该阅读 Flask 文档中的整个快速入门:http: //flask.pocoo.org/docs/quickstart/

于 2012-10-18T18:55:22.663 回答
1

您尚未发布任何有关错误发生位置的信息 :) 而且我看不到您尝试访问的位置self.requests

查看其他 Flask 应用程序,您似乎应该直接访问request

看看这些例子:https ://github.com/mitsuhiko/flask/tree/master/examples

于 2012-10-18T18:55:01.957 回答