python - 带有 GET 参数的 GAE ValueError

标签 python google-app-engine get

我正在尝试创建一个简单的应用程序来返回给定 GET 参数的两个输入的余数。这些输入将从 url 查询中获取。

例如http://thisisanexample.appspot.com/?a=1&b=2 这应该导致答案 1,因为 2/1 的余数 = 1。

    a = self.request.get('a')
    b = self.request.get('b')
    c = 0
    if (int(a)>int(b)):
        c=int(a)%int(b)
    else:
        c=int(b)%int(a)
    self.response.out.write(type(a))

但是,我遇到了以下 ValueError 问题:ValueError: invalid literal for int() with base 10: ''

我假设问题在于变量以前是未知的,并且可能开始或如果未指定,则保持为 '',从而产生错误。任何帮助将不胜感激!

最佳答案

GET参数保存在request.GET中,而不是请求本身,所以你想把你的代码改成

a = request.GET.get('a')
b = request.GET.get('b')

顺便说一句,你在哪里写的代码?什么是 self.request?如果您正在编写一个 View ,它应该只是:

def my_view(request, *args, **kwargs ):
    # access request here

request.get 没有在 Django HttpRequest 对象上定义...

:编辑:

GAE 不完全是 Django。引用 GAE's documentation :

By default, get() returns the empty string ('')

因此您的代码是正确的,但您应该检查空字符串,而不是检查 ab 是否为 None。 p>

关于python - 带有 GET 参数的 GAE ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9564826/

相关文章:

python - openpyxl - "copy/paste"单元格范围

python - 如何在 Python Flask Web 应用程序中嵌入 DataTable 小部件

python - 以 django 形式动态填充值

python - NDB/DB NoSQL 注入(inject) Google 数据存储

php - 使用 $_GET

python - 理解 Python 类和对象

python - 在 python __del__ 处理过程中,基类在子类之前被删除

python - Google Appengine Ndb GQL 查询最大限制是多少?

http - 为什么查询字符串在 GET 请求的 URL 中发送并在 POST 请求的正文中发送?

php - 如何使用 PHP 在 URL 中传递 URL(作为 GET 参数)?