【发布时间】:2012-10-09 07:43:29
【问题描述】:
我正在制作一个股票应用程序,在用户输入 MSFT 等股票后,他们将被重定向到 .../stock?s=MSFT 我已经成功制作了这部分,但现在我需要 Python 来获取这个最新的、唯一的URL,以便我可以解析引用(在本例中为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')
非常感谢。
【问题讨论】:
-
请提供更多代码。看起来你正在混合方法和功能:)
-
@MortenJensen 谢谢;我已经更新了我的答案
-
我添加了一个答案。如果您已经在
home_search中使用了request对象,您究竟为什么会想到self.request? -
@MarkusUnterwaditzer 很好的问题,但感谢您的回答和解释。