如何在发送到 Jinja2 模板之前处理查询?应用引擎
How to process a query before sending to Jinja2 template? App Engine
我对如何在将查询结果发送到 Jinja2 以在浏览器上呈现之前处理它感到困惑。
我正在 Google App Engine 上为 Python 开发应用程序。
我将工作时间存储在 "calendar" 实体中作为秒数,这样我就可以对时间进行一些计算。我遇到的问题是如何修改查询结果,以便我传递给 Jinja2 html 文件的时间不是以秒(整数)而是以 HH:MM(字符串)
为单位
我的models.py文件如下:
'calendars_list': calendars_list,
class Calendar(ndb.Model):
wtd1 = ndb.IntegerProperty() # Working seconds day 1
wtd2 = ndb.IntegerProperty() # " 2
wtd3 = ndb.IntegerProperty() # " 3
wtd4 = ndb.IntegerProperty() # " 4
wtd5 = ndb.IntegerProperty() # " 5
wtd6 = ndb.IntegerProperty() # " 6
wtd7 = ndb.IntegerProperty() # " 7
在主请求处理程序中,我得到查询(在定义祖先等之后)并且需要在浏览器中呈现之前将秒数更改为 HH:MM。
calendars_query = models.Calendar.query(ancestor = get_calendars_key()).order(models.Calendar.calendar_id)
calendars_list = calendars_query.fetch(10)
# Now: convert working times per day (sec) to HH:MM
for calendar in calendars_list:
calendar.wtd1 = format_as_HHMM(calendar.wtd1)
--> Gives an error: calendar.wtd1 needs to be an integer, can't be a string
template_values = {
'calendars_list': calendars_list,
}
template = JINJA_ENVIRONMENT.get_template('calendars.html')
self.response.write(template.render(template_values))
如上所示,当我修改 calendar.wtd1 元素以将其从 say 3600 更改为 01:00 时出现错误,因为它需要是一个整数。
关于如何更改代码以便正确处理查询结果的任何想法?
谢谢!
您的代码的问题是您尝试将一个非整数值分配给 ndb.IntegerProperty()。
替换
calendar.wtd1 = format_as_HHMM(calendar.wtd1)
和
calendar.wtd1_HHMM = format_as_HHMM(calendar.wtd1)
并在模板中使用 calendar.wtd1_HHMM,它可能会很好地工作。
编辑:
如果您更喜欢在模板中将整数转换为 HH:mm 作为表示逻辑的一部分,您可以通过编写转换函数并将其注册为过滤器来轻松做到这一点,如 documentation - writing filters [=14 中所述=]
当涉及到进行转换的函数时,您很幸运:
Python: convert seconds to hh:mm:ss
我对如何在将查询结果发送到 Jinja2 以在浏览器上呈现之前处理它感到困惑。 我正在 Google App Engine 上为 Python 开发应用程序。
我将工作时间存储在 "calendar" 实体中作为秒数,这样我就可以对时间进行一些计算。我遇到的问题是如何修改查询结果,以便我传递给 Jinja2 html 文件的时间不是以秒(整数)而是以 HH:MM(字符串)
为单位我的models.py文件如下:
'calendars_list': calendars_list,
class Calendar(ndb.Model):
wtd1 = ndb.IntegerProperty() # Working seconds day 1
wtd2 = ndb.IntegerProperty() # " 2
wtd3 = ndb.IntegerProperty() # " 3
wtd4 = ndb.IntegerProperty() # " 4
wtd5 = ndb.IntegerProperty() # " 5
wtd6 = ndb.IntegerProperty() # " 6
wtd7 = ndb.IntegerProperty() # " 7
在主请求处理程序中,我得到查询(在定义祖先等之后)并且需要在浏览器中呈现之前将秒数更改为 HH:MM。
calendars_query = models.Calendar.query(ancestor = get_calendars_key()).order(models.Calendar.calendar_id)
calendars_list = calendars_query.fetch(10)
# Now: convert working times per day (sec) to HH:MM
for calendar in calendars_list:
calendar.wtd1 = format_as_HHMM(calendar.wtd1)
--> Gives an error: calendar.wtd1 needs to be an integer, can't be a string
template_values = {
'calendars_list': calendars_list,
}
template = JINJA_ENVIRONMENT.get_template('calendars.html')
self.response.write(template.render(template_values))
如上所示,当我修改 calendar.wtd1 元素以将其从 say 3600 更改为 01:00 时出现错误,因为它需要是一个整数。
关于如何更改代码以便正确处理查询结果的任何想法?
谢谢!
您的代码的问题是您尝试将一个非整数值分配给 ndb.IntegerProperty()。
替换
calendar.wtd1 = format_as_HHMM(calendar.wtd1)
和
calendar.wtd1_HHMM = format_as_HHMM(calendar.wtd1)
并在模板中使用 calendar.wtd1_HHMM,它可能会很好地工作。
编辑: 如果您更喜欢在模板中将整数转换为 HH:mm 作为表示逻辑的一部分,您可以通过编写转换函数并将其注册为过滤器来轻松做到这一点,如 documentation - writing filters [=14 中所述=]
当涉及到进行转换的函数时,您很幸运: Python: convert seconds to hh:mm:ss