简体   繁体   English

Python Google App Engine Jinja2找不到包含

[英]Python Google App Engine Jinja2 Can't find Include

I'm new to google app engine and python development and am trying to use Jinja2 as my templating engine. 我是google app引擎和python开发的新手,正在尝试使用Jinja2作为模板引擎。 I have a page that I'm trying to "include" a header and footer on and I keep getting this error: TemplateNotFound: base/header.html 我有一个页面试图在其上“包括”页眉和页脚,并且不断出现此错误:TemplateNotFound:base / header.html

My code is below. 我的代码如下。 Please let me know if you have any suggestions. 如果您有任何建议,请告诉我。

Python 蟒蛇

JINJA_ENVIRONMENT = jinja2.Environment(
   loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
   extensions=['jinja2.ext.autoescape'])

class MainPage(webapp.RequestHandler):


    def get(self):
        template_values = {'title' : 'Test Value'}
        template = JINJA_ENVIRONMENT.get_template('/public/templates/index.html')
        self.response.write(template.render(template_values))

Configuration 组态

libraries:
- name: webapp2
  version: latest
- name: jinja2
  version: latest

File Structure 档案结构

- my application
  - public
    - scripts
    - stylesheets
    - templates
      - base
        - header.html
        - footer.html
      - index.html
  - app.yaml
  - myapplication.py

HTML (index.html) HTML(index.html)

{% include "base/header.html" %}
<nav class="top-bar" style="">
  <!-- more html here -->
</nav>
{% include "base/footer.html" %}

I have not used the loader property for jinja2 so i am not sure what this line does 我没有使用jinja2的loader属性,所以我不确定这行会做什么

loader=jinja2.FileSystemLoader(os.path.dirname(__file__))

But i do use App Engine successfully with jinja2 so i think i can help. 但是我确实与jinja2一起成功使用了App Engine,因此我认为我可以提供帮助。 I know by default jinja2 looks for a directory named "templates" in the root of you project. 我知道默认情况下jinja2在您项目的根目录中查找名为“ templates”的目录。 So for the header.html jinja2 would expect that file to be in "templates/base/header.html". 因此对于header.html,jinja2希望该文件位于“ templates / base / header.html”中。

If you look in environment.py where the get_template function is for jinja2 if throws a 如果您在environment.py中查找get_template函数用于jinja2的位置,则抛出

    If the template does not exist a :exc:`TemplateNotFound` exception is
    raised.

Create a templates directory in the root of your app engine project and move all jinja2 templates into there and let me know if that fixes it. 在您的App Engine项目的根目录中创建一个模板目录,并将所有jinja2模板移至该目录,并告诉我是否可以解决该问题。 When you pass the template names to jinja2 they will all be relative to the folder so call should look like 当您将模板名称传递给jinja2时,它们都将相对于文件夹,因此调用应类似于

template = JINJA_ENVIRONMENT.get_template('index.html')

with index.html being in /templates/index.html in your root. index.html位于您根目录下的/templates/index.html中。

If it does i can show you how to customize the jinja2 default config by passing a dictionary to it. 如果可以的话,我可以向您展示如何通过将字典传递给它来定制jinja2默认配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM