2012-08-24 37 views
1

我在根目錄的「靜態」子目錄中有一個html文件「listagem.html」。 我想使用「listagem.html」作爲jinja2的模板。GAE/Python/jinja2 /如何引用加入語句中的子目錄

我想這3個加入公式:

第一:

jinja_environment = jinja2.Environment(
    autoescape = True, 
    loader = jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'static'))) 

二:

jinja_environment = jinja2.Environment(
    autoescape = True, 
    loader = jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'static/'))) 

三:

jinja_environment = jinja2.Environment(
    loader = jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), '/static'))) 
     template = jinja_environment.get_template('listagem.html') 
     self.response.out.write(template.render(template_values)) 

,並收到此錯誤:

file not accessible: 'C:\\Users\\Me\\AppEngine\\MyAppRoot\\static\\listagem.html' 

我在做什麼錯?

坦克求救。

回答

5

你可能已經在你app.yaml文件添加一個static_dir URL處理器,並且已經設置你static目錄(你模板)作爲static_dir

這使得您的文件無法訪問,因爲static files are not available in the application's file system

app.yaml文件中刪除static_dir並在項目文件夾中添加一個static-templates文件夾。

創建一個神社的環境如下:

jinja_environment = jinja2.Environment(autoescape=True, 
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'static'))) 
+0

可以接受的答案,如果它的工作,幫助別人誰看的問題 – thikonom

+0

坦克的幫助。它運行良好。 – RRBaldi

+0

只是爲了擴大這個看起來很明顯的東西,但如果一個文件與'skip_files'正則表達式匹配,你也會得到'IOError'。所以,它不能是靜態的,路徑不應該被跳過。 – hjc1710

0

你有一個錯誤。 1.在你的AppEngine上的配置文件,你需要同樣的代碼那樣:

application: yourapp 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: yes 

handlers: 
- url: /favicon\.ico 
    static_files: favicon.ico 
    upload: favicon\.ico 
- url: /js 
    static_dir: static/js 
- url: /css 
    static_dir: static/css 
- url: /img 
    static_dir: static/img 
- url: .* 
    script: main.app 

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

看說:「 - 網址項目」 ...這是你的靜態內容(JS CSS IMG) 您的HTML模板,你需要一個你的根應用程序的子文件夾模板,在我的情況是myapp /模板,並在裏面把你的模板(模板內容,HTML)

你的主應用程序看起來像那樣。 在main.py

import os 
import webapp2 
import jinja2 

jinja_environment = jinja2.Environment(autoescape=True, 
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates'))) 

class MainHandler(webapp2.RequestHandler): 
    def get(self): 
     template_values = { 
      'name': 'nombre', 
      'verb': 'programando' 
     } 

     template = jinja_environment.get_template('index.html') 
     self.response.out.write(template.render(template_values))  

app = webapp2.WSGIApplication([ 
    ('/', MainHandler) 
], debug=True) 
相關問題