推荐学习书目
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
ryan123
V2EX  ›  Python

flask 中如何实现主题点击数 和 发表的时间显示 xx 时间之前

  •  
  •   ryan123 · Jun 15, 2014 · 5176 views
    This topic created in 4365 days ago, the information mentioned may be changed or developed.
    11 replies    2014-06-15 21:15:11 +08:00
    passluo
        1
    passluo  
       Jun 15, 2014   ❤️ 1
    第二个需求可以用这个:
    https://github.com/crsmithdev/arrow
    da_a
        2
    da_a  
       Jun 15, 2014   ❤️ 1
    我这么做的,在model里加入:

    @property
    def last_time(self):
    now = datetime.utcnow()
    last = now - self.create_at
    if last.days > 0:
    return str(last.days) + u'天前'
    else:
    if last.seconds > 3600:
    return str(last.seconds / 3600) + u'小时前'
    elif last.seconds > 60:
    return str(last.seconds / 60) + u'分钟前'
    else:
    return u'刚刚'
    ryan123
        3
    ryan123  
    OP
       Jun 15, 2014
    @passluo 谢谢,第一个如何实现好?
    ryan123
        4
    ryan123  
    OP
       Jun 15, 2014
    @da_a 谢谢, 你这个很不错哦
    ryan123
        5
    ryan123  
    OP
       Jun 15, 2014
    @da_a 已经成功用上你的方法,再次感谢
    da_a
        6
    da_a  
       Jun 15, 2014
    @ryan123 不客气
    skybr
        7
    skybr  
       Jun 15, 2014   ❤️ 2
    我建议直接返回正常时间, 相对时间的显示用moment.js在前端处理, 还能setInterval定时刷.
    lix
        8
    lix  
       Jun 15, 2014
    flask-moment
    cbsw
        9
    cbsw  
       Jun 15, 2014   ❤️ 1
    直接给 topic 加一个 clicked 字段,访问一次,就加一
    @skybr +1 时间显示直接交给前端,jQuery 有个 timeago 插件 http://timeago.yarp.com/
    ryan123
        10
    ryan123  
    OP
       Jun 15, 2014
    @cbsw 谢谢
    ryan123
        11
    ryan123  
    OP
       Jun 15, 2014
    总结下,给像为一样的新手:
    时间: Flask-Moment
    点击数:添加一个字段,每访问一次加一次
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   945 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 21:38 · PVG 05:38 · LAX 14:38 · JFK 17:38
    ♥ Do have faith in what you're doing.