【问题标题】:Getting HTTP GET variables using Tipfy使用 Tipfy 获取 HTTP GET 变量
【发布时间】:2011-02-03 22:07:52
【问题描述】:

我目前正在 Google 的 Appengine 上使用 tipfy,最近遇到了一个问题:我一生都找不到关于如何在我的应用程序中使用 GET 变量的任何文档,我已经尝试过筛选tipfyWerkzeug's 文档均未成功。我知道我可以使用request.form.get('variable') 来获取POST 变量,并在我的处理程序中使用**kwargs 来获取URL 变量,但这与文档告诉我的一样多。有什么想法吗?

【问题讨论】:

  • 您在考虑 URL 变量和 GET 变量之间的区别吗?

标签: python google-app-engine mod-wsgi werkzeug tipfy


【解决方案1】:

request.args.get('variable') 应该适用于我认为您所说的“获取数据”。

【讨论】:

  • 在 Tipfy 的教程 (tipfy.org/wiki/tutorials/sessions) 中,他们使用这种语法:request.args.get('variable', None) 额外的 'None' 是干什么用的?
  • @Wraith,他们可能将其添加为“显式优于隐式”的应用程序-在这种情况下,我不同意,因为我不认为它是显式的而是多余的(但我确实添加了一个显式或冗余的return None 在沿其他路径返回其他值的函数的末尾)。无论如何,这是一个严格的文体问题,没有任何语义含义。
【解决方案2】:

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

Request 对象包含应用程序客户端传输的所有信息。您将从它检索 GET 和 POST 值、上传的文件、cookie 和标头信息等等。所有这些东西都很常见,你会很习惯的。

要访问 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】:

    这对我有用(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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 2013-07-13
      相关资源
      最近更新 更多