最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask中的templates的html中格式化datetime

Flask crifan 5421浏览 0评论

现在已有对应的Flask的templates的html中的python的datetime变量:

todayEvent.start_date

{% for todayEvent in todayEventList %}
<li>
    <a href="showEvent.html" class="item-link item-content">
        <div class="item-content">
            <div class="item-media">
                <img class="initiator-avatar" src="{{ url_for(‘static’, filename=’img/avatar/liyuanfei.jpg’) }}">
            </div>
            <div class="item-inner">
                <div class="item-title-row">
                    <div class="item-title">{{ todayEvent.title }}</div>
                </div>
                <div class="item-subtitle">发起人:{{ todayEvent.user.nickname }}</div>
                <div class="item-subtitle">时间:{{ todayEvent.start_date }} – {{ todayEvent.end_date }}</div>
                <div class="item-subtitle">地点:{{ todayEvent.location }}</div>
                <div class="item-subtitle">已报名:{{ todayEvent.cur_user_num }} / {{ todayEvent.max_user_num }}</div>
            </div>
        </div>
    </a>
</li>
{% endfor %}

现在输出的结果是:

2016-08-24 20:11:19.25024

而现在希望:

在Flask的Jinja2的模版中,对于datetime类型的:

todayEvent.start_date

去格式化输出字符串为:

8/24 20:11

即可。

不知道,是不是直接按照Python的写法,去处理datetime的变量,即可?

还是有啥其它特殊的写法。

flask jinja2 date format

Python: How do I format a date in Jinja2? – Stack Overflow

API — Jinja2 Documentation (2.8-dev)

Templating With Jinja2 in Flask: Date and Time Formatting With moment.js

How should I format dates in jinja? – Quora

List of Builtin Filters

flask中模板日期格式控制和jinja2中模板格式控制 – baoyiluo – 博客园

miguelgrinberg/Flask-Moment: Formatting of dates and times in Flask templates using moment.js.

Flask-Babel — Flask Babel 1.0 documentation

Primer on Jinja Templating – Real Python

brahici’s lair : Create Jinja2 template filters in Flask

最后是:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/templates/index.html

中:

<div class="item-subtitle">时间:{{ todayEvent.start_date | datetime_format }} – {{ todayEvent.end_date | datetime_format }}</div>

以及:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/views.py

import datetime
@app.template_filter(‘datetime_format’)
def _jinja2_filter_datetime_format(datetimeValue, format=’%M/%d %H:%m’):
    """convert a datetime to a different format."""
    return datetimeValue.strftime(format)

即可输出:

11/24 20:08

但好像时间不对,不是希望的:

8/24 20:08

所以去搞清楚:

[已解决]Python中用strftime格式化datetime出错

然后即可正常输出:

[总结]

1.此处,用:

(比如放在Flask中的views.py中,即可)

import datetime
@app.template_filter(‘datetime_format’)
def _jinja2_filter_datetime_format(datetimeValue, format=‘%m/%d %H:%M’):
    """convert a datetime to a different format."""
    return datetimeValue.strftime(format)

然后templates中的html中,去调用:

<div class="item-subtitle">时间:{{ todayEvent.start_date | datetime_format }} – {{ todayEvent.end_date | datetime_format }}</div>

即可。

2.如果想要自定义格式化字符串,可以用:

{{ todayEvent.start_date | datetime_format(“%y%m%d %H%M%s") }}

3.另外,对于上述的:

@app.template_filter(‘datetime_format’)
def _jinja2_filter_datetime_format(datetimeValue, format=‘%m/%d %H:%M’):

其实可以换成:

@app.template_filter()
def datetime_format(datetimeValue, format=‘%m/%d %H:%M’):
。。。
app.jinja_env.filters[‘datetime_format’] = datetime_format

4.以后有机会可以去试试:

miguelgrinberg/Flask-Moment: Formatting of dates and times in Flask templates using moment.js.

中的。

其中包括,更高级的,自动刷新时间的refresh

以及设置语言的lang

5.抽空还可以去试试:

flask中模板日期格式控制和jinja2中模板格式控制 – baoyiluo – 博客园

提到的:

import jinja_filters
          app =Flask(__name__)
          app.jinja_env.filters[‘filter_name1’]= jinja_filters.filter_name1
          app.jinja_env.filters[‘filter_name2’]= jinja_filters.filter_name2

-》

估计可以把jinja的filters,单独放到对应py文件中,更加模块化,逻辑更佳清晰。

6.以及也可以抽空去用用那个Babel:

看起来是支持多国语言的。

Flask-Babel — Flask Babel 1.0 documentation

“Flask-Babel is an extension to Flask that adds i18n and l10n support to any Flask application with the help of babel, pytz and speaklater. It has builtin support for date formatting with timezone support as well as a very simple and friendly interface to gettext translations.”

转载请注明:在路上 » [已解决]Flask中的templates的html中格式化datetime

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.221 seconds, using 22.05MB memory