为什么GAE没有找到我的模板文件夹中的文件?

时间:2012-03-11 18:00:34

标签: python google-app-engine jinja2

我很困惑。昨晚工作顺利,今天突然停止了工作。

此页面的目的是生成颜色列表。模板位于名为“templates”的自己的文件夹中,但GAE似乎根本无法找到这些模板。

我在这里做错了什么?

main2.py:

import bottle
from bottle import static_file
from google.appengine.ext.webapp import util
from bottle import route

# Load the template system
from jinja2 import Environment, FileSystemLoader

# Indicate from where the templates will be loaded
env = Environment(loader=FileSystemLoader('./templates/'))

# for randomly picking colors
import random

colors = 'green red blue';

@route('/favicon.ico')
def send_image():
    filename = 'favicon.ico'
    return static_file(filename, root='./images/', mimetype='image/ico');

@route('/hello')
def hello():
    template = env.get_template('home.html');
    color_list = colors.split();
    num_colors = random.randint(0,len(color_list)+1);
    color_list = color_list[:num_colors];
    return template.render(title="Color List Page!", color_list=color_list);

util.run_wsgi_app(bottle.default_app())

的app.yaml

application: yao-webapp2
version: 1
api_version: 1
runtime: python

handlers:
- url: .*
  script: main2.py

/templates/base.html

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <title>
        {% block title %}
        I am stupid as heck: I forgot to fill in a title.
        {% endblock %}
    </title>
    </head>
    <body>
        {% block content %}
        No body knows the trouble I've seen.
        {% endblock %}
    </body>
</html>

/templates/home.html

{% extends "base.html" %}

{% block title %}
    {{page_title}}
{% endblock %}

{% block content %}
    <h1>Some Colors I know </h1>
    <p>I have a list of colors that I know</p>
    {% if color_list %}
        <ul>
        {% for color in color_list %}
            <li> {{ color }} </li>
        {% endfor %}
        </ul>
    {% else %}
        <p>Oops! No colors.</p>
    {% endif %}
{% endblock %}

1 个答案:

答案 0 :(得分:2)

不确定你得到了什么错误,但我会尝试使用绝对路径。

而不是

env = Environment(loader=FileSystemLoader('./templates/'))

尝试使用此代码:

templatespath = os.path.join(os.path.dirname(__file__), "templates")
env = Environment(loader=FileSystemLoader(templatespath))