自定义时间过滤器

2019-05-27  本文已影响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>
上一篇 下一篇

猜你喜欢

热点阅读