代码之家  ›  专栏  ›  技术社区  ›  sgerbhctim

为什么网页没有呈现代码

  •  -1
  • sgerbhctim  · 技术社区  · 8 年前

    出于某种原因-我没有得到任何错误,但变量是空白的


    你好,测试!

    当地的日期和时间是。

    那是。


    网站网站

    from flask_moment import Moment
    from datetime import datetime
    
    app = Flask(__name__)
    moment = Moment(app)
    
    @app.route('/index')
    def index():
            return render_template('index.html', current_time=datetime.utcnow())
    

    模板/index.html

    <h3>Hello, test!</h3>
    
    <p>the local date and time is {{ moment(current_time).format('LLL') }}.</p>
    <p>that was {{ moment(current_time).fromNow(refresh=True) }}.</p>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Kaiser Dandangi    8 年前

    如果你看看 flask_moment Readme ,步骤1。说你需要用你的flask应用程序来初始化扩展,比如:

    from flask_moment import Moment
    from datetime import datetime
    
    # initialize you flask app
    app = Flask(__name__)
    # initialize the moment extension
    Moment(app)
    
    @app.route('/index')
    def index():
        return render_template('index.html', current_time=datetime.utcnow())
    

    然后第2步。正在将此包含在模板中:

    <head>
        {{ moment.include_jquery() }}
        {{ moment.include_moment() }}
    </head>
    

    这就是模板呈现为空白的原因;模板模块无法识别矩函数。