2017-02-13 59 views
0

在我最近的项目中,我选择了flask-moment而不是moment.js。但是,它一直在出现UndefinedError: 'moment' is undefined时刻在瓶子时刻未定义

我使用工厂模式是这样的:

from config import config 
from exts import babel, db, login_manager, mail, moment 

def create_app(config_name): 

    app = Flask(__name__) 
    app.config.from_object(config[config_name]) 

    ... 

    moment.init_app(app) 

    return app 

我不添加{{ moment.include_moment() }}base.jinja2文件的末尾,它的成功加载。我写了一个components.jinja2包含这段代码:

{% macro render_file_thumbnail(file) %} 
<div class=""> 
    <hr> 
    <div class="media"> 
     <div class="media-left"> 
      <a href="{{ url_for('people.profile', user_id=file.uploader.id) }}"> 
       <img class="media-object file-th-avatar img-rounded" src="{{ file.uploader.avatar }}" alt="{{ file.uploader.nickname }}"> 
      </a> 
     </div> 
     <div class="media-body"> 
      <h6 class="file-th-header"> 
       <a href="{{ url_for('file.file_display', page_number=1, file_id=file.id) }}"> 
        {{ file.file_name }} 
       </a> 
      </h6> 
      <p> 
       <span class="text-muted fa fa-fw fa-institution"></span> 
       <a href="{{ url_for('course.course', course_id=file.course.id, page_number=1) }}"> 
        {{ file.course.course_name }} 
       </a> 
       <span class="text-muted fa fa-fw fa-pencil"></span> 
       {{ file.author }} 
       <span class="text-muted fa fa-fw fa-download"></span> 
       {{ file.downloaded_times}} {{ _('time(s)') }} 
       <span class="text-muted fa fa-fw fa-clock-o"></span> 
       {{ moment(file.uploaded_time).fromNow(refresh=True) }} 
      </p> 
     </div> 
    </div> 
</div> 
{% endmacro %} 

我将其导入呈现模板,错误出现。

但是如果我用{% include %}而不是marco,问题就解决了!那么我该如何继续使用marco并同时解决这个问题呢?

谢谢!

回答

1

如果您没有指示上下文,则不传递给宏。您可以通过导入与宏观背景下

{% from "your_macro_file" import render_file_thumbnail with context %} 

解决这个问题请参考docthis question

+0

它的工作原理。谢谢! –