使用Tipfy获取HTTP GET变量

2 投票
3 回答
1587 浏览
提问于 2025-04-15 21:10

我最近在谷歌的Appengine上玩一个叫做tipfy的东西,遇到了一个问题:我怎么也找不到关于如何在我的应用中使用GET变量的文档。我试着翻阅了tipfyWerkzeug的文档,但都没有找到有用的信息。我知道可以用request.form.get('variable')来获取POST变量,也可以在我的处理程序中用**kwargs来获取URL变量,但文档就只告诉我这些。有没有什么好的建议呢?

3 个回答

0

这个对我有效(tipfy 0.6版本):

from tipfy import RequestHandler, Response

from tipfy.ext.session import SessionMiddleware, SessionMixin

from tipfy.ext.jinja2 import render_response

from tipfy import Tipfy

class I18nHandler(RequestHandler, SessionMixin):
    middleware = [SessionMiddleware]
    def get(self):
        language = Tipfy.request.args.get('lang')
        return render_response('hello_world.html', message=language)
2

来源: http://www.tipfy.org/wiki/guide/request/

Request对象包含了应用程序客户端发送的所有信息。你可以从中获取GET和POST的值、上传的文件、cookies(小数据文件)以及头部信息等等。这些内容非常常见,你会很快习惯它们。

要访问Request对象,只需从tipfy中导入request变量即可:

from tipfy import request

# GET
request.args.get('foo')

# POST
request.form.get('bar')

# FILES
image = request.files.get('image_upload')
if image:
    # User uploaded a file. Process it.

    # This is the filename as uploaded by the user.
    filename = image.filename

    # This is the file data to process and/or save.
    filedata = image.read()
else:
    # User didn't select any file. Show an error if it is required.
    pass
3

request.args.get('variable') 这个代码应该能满足你所说的“获取数据”的需求。

撰写回答