【问题标题】:Create template object to display dependent on time of day创建模板对象以根据一天中的时间显示
【发布时间】:2010-11-20 22:29:19
【问题描述】:

我需要创建一个跨站点模板对象,该对象将检查当前时间,如果它在模型中指定的时间范围内,则返回一个字符串,或者在所有其他情况下返回空白。

看起来很简单,但我想知道这里最好的方法是什么?此外,还有一些其他注意事项:

  • 字符串应该是可编辑的
  • 显示时间的结束时间不应早于开始时间
  • 我们需要允许存储多个字符串,但只有一个(或没有)被选为“实时”

作为起点,我会使用模型来定义字符串以及开始和结束时间,如下所示:

from datetime import datetime
class dynamicString(models.Model):
start = models.TimeField()
end = models.TimeField()
name = models.CharField(max_length=50, help_text = 'Just for reference, won\'t be displayed on site.')
number = models.CharField(max_length=18, help_text = 'This is the string to be displayed within the above timeframe.')
active = models.BooleanField(help_text = 'Uncheck this to stop string from displaying entierly.')
def __unicode__(self):
    return self.name

但是接下来在哪里合并逻辑规则呢?

【问题讨论】:

    标签: django datetime django-models django-templates


    【解决方案1】:

    使用更简单的规则系统重新设计了这个:检查一个数字时间范围,如果在范围内并且如果不为空,则返回该数字,否则返回一个备用数字(可能为空)。

    处理作为上下文进程传递的处理。

    【讨论】:

      猜你喜欢
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 2020-03-14
      相关资源
      最近更新 更多