查询字符串出现“无效字面量,无法转换为十进制整数”的错误

2 投票
2 回答
849 浏览
提问于 2025-04-17 00:10

我之前试着从数据存储中获取一个模型对象,使用的是从查询字符串中传来的键。之前这个方法是有效的,但最近我把文件名从 restaurantProfile.html 改成了 restaurant_profile.html。现在我不知道哪里出了问题,导致这个功能不再正常。

我的餐厅键应该包含这个键。不过,从 GAE 的日志来看,我设置了自定义调试,想看看传给我的处理程序(Result)的餐厅键里包含了什么:

GAE Log:
Restaurant Key = <bound method Restaurant.key of <persistence.model.Restaurant object at 0x5cd3c5acd5768900>>

有没有人能告诉我这里出了什么问题?餐厅键似乎没有传递给处理程序。

<a href="/restaurant_profile?restaurant_key={{ key }}">

处理程序类:

class RestaurantProfileHandler(webapp.RequestHandler):
def get(self):
    restaurant_key = self.request.GET.get(u'restaurant_key')

    logging.debug('Restaurant Key = %s', restaurant_key)

错误信息:“invalid literal for int() with base 10”,出现在以下代码:

def get_RestaurantDetails(restaurant_key):
   if restaurant_key:
       restaurant = db.Key.from_path('Restaurant', int(restaurant_key))
       return db.get(restaurant)

获取键的代码:

for restaurant in foundRestaurants:
            result_data[restaurant.key] = restaurant

问题通过以下方法解决:

 key = restaurant.key().__str__()
            result_data[key] = restaurant

2 个回答

0

有人能告诉我这里出了什么问题吗?

问题就出在这里:restaurant_key 是一个方法。你不能直接对它使用 int();你是不是想对调用 restaurant_key() 的结果使用 int() 呢?

至于为什么 restaurant_key 是一个方法,这就是 self.request.GET.get(u'restaurant_key') 返回的结果。也许你应该再看看文档……

1

问题通过以下方式解决:

 key = restaurant.key().__str__()
            result_data[key] = restaurant

撰写回答