V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chengxiao
V2EX  ›  Python

求助一下 关于在 DJango 中的时间筛选

  •  
  •   chengxiao · 2017-11-28 22:48:27 +08:00 · 2120 次点击
    这是一个创建于 2334 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在做一个日常打卡的项目,碰到了一点问题,日期到第二天不能正确辨认

    today = timezone.now.date()
    def log:
        log = Logs.object.filter(date=today)
        if log:
            do something
        else:
            do somethng
    

    当我在服务器运行时,第一次打卡是正确的,可是到第二天的时候,理论上应该出现 else 的情况,可是却不出来,必须重启 uwsgi 才可以,请教下这是什么问题呢?

    5 条回复    2017-11-29 10:59:32 +08:00
    Cynic222
        1
    Cynic222  
       2017-11-29 09:19:47 +08:00 via iPhone
    虽然没看到完整代码,我觉得 today=...不应该放在 def 外面
    vZexc0m
        2
    vZexc0m  
       2017-11-29 09:21:20 +08:00
    因为这里的 today 成了定值
    chengxiao
        3
    chengxiao  
    OP
       2017-11-29 09:29:14 +08:00
    @Cynic222
    @vZexc0m
    似乎是这样 昨晚也发现问题可能出现在这个全局变量上,今天正常啦~
    Marmot
        4
    Marmot  
       2017-11-29 10:02:48 +08:00
    你初始化的时候 today (全局变量)就被定死了,你放在里面的话,每次运行函数才指定 today,所有才正确。
    shuizhengqi
        5
    shuizhengqi  
       2017-11-29 10:59:32 +08:00
    这种东西只是 python 语法问题,跟 django 没关系。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1520 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:03 · PVG 01:03 · LAX 10:03 · JFK 13:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.