美文网首页
自定义时间过滤器

自定义时间过滤器

作者: 鬼会画符 | 来源:发表于2019-05-27 19:52 被阅读0次

.py文件

from flask import Flask,render_template,request,redirect

from datetime import datetime

app = Flask(__name__)


@app.route('/',methods=['GET','POST'])
def index():
    if request.method == 'GET':
        return render_template('index1.html')
    else:
        account = request.form.get('account')
        password = request.form.get('password')
        if account == '123' and password == '123':
            #调用article函数
            return redirect("article")
        else:
            return render_template('index1.html')

@app.route('/article',methods=['GET','POST'])
def article():

    ctx = {
        'data':[
            {'title':'吧波蹦','time':datetime.now(),'content':'吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦'},
            {'title':'吧波蹦','time':datetime.now(),'content':'吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦'},
            {'title':'吧波蹦','time':datetime.now(),'content':'吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦吧波蹦'},
        ]
    }


    return render_template('article.html',**ctx)
#自定义过滤器
# now_time=time.strftime('%Y-%m-%d',time.localtime(time.time()))

def handletime(time,mode):
    #把上边的strftime方法封装一下,具体格式可以在html页面中设定
    return time.strftime(mode)
#注册自定义的过滤器
app.jinja_env.filters['handletime'] = handletime

if __name__ == '__main__':
    app.run(debug = True)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<form action='' method="post">
    账号:<input type="text" name="account"><br>
    密码:<input type="password" name="password"><br>

    <input type="submit" value="提交"> </form>

</body>

article.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul>

    {% for i in data %}
    <li>
        <h1>标题:{{ i.title }}</h1>
        <h1>时间:{{ i.time|handletime('%Y*%m*%d') }}</h1>
        <h1>正文:{{ i.content }}</h1>
    </li>

    <hr>
{% endfor %}

</ul>

</body>
</html>

相关文章

  • vue filter 过滤器使用

    格式化时间 自定义全局过滤器 vue 自定义过滤器分为"全局过滤器"和"局部过滤器"两种。 一、 全局过滤器 全局...

  • 自定义时间过滤器

    内置的时间过滤器效果 {{curDate | date}} 自定义的时间过滤器效果及代码 {{curDate | ...

  • 2017-5-25 AngularJs

    service 自定义服务 1.指令 内置指令 自定义指令 2.过滤器 内置过滤器 自定义过滤器 3.服务 内置服...

  • 自定义过滤器的封装

    封装自定义过滤器 引入过滤器 添加+注册过滤器 使用过滤器

  • angular 过滤器的几种用法

    自定义过滤器: 自定义服务后,连接到应用上,在过滤器中使用:

  • 3 自定义(重要)

    循环 过滤器 自定义过滤器: filter 自定义指令 directive * 注意: 必须以 v-开头 自定...

  • filter

    时间格式转换https://segmentfault.com/a/1190000000481753 自定义过滤器h...

  • vue自定义过滤器

    自定义过滤器

  • Vue与Angular以及React的区别?

    1、与AngularJS的区别 相同点:都支持指令:内置指令和自定义指令;都支持过滤器:内置过滤器和自定义过滤器;...

  • Vue日期格式化与过滤器

    自定义过滤器格式化时间格式(用到了moment.js 库)

网友评论

      本文标题:自定义时间过滤器

      本文链接:https://www.haomeiwen.com/subject/weektctx.html