GAE / Python / jinja2 / Cómo hacer referencia a un subdirectorio en la declaración de unión

I have a html file "listagem.html" in the subdirectory "static" of my root directory. I want to use "listagem.html" as a templates for jinja2.

I tried these 3 join formulas:

Primero:

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

Segundo:

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

Tercero:

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)) 

y recibió este error:

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

¿Qué estoy haciendo mal?

Tanks for help.

preguntado el 24 de agosto de 12 a las 23:08

2 Respuestas

You have probably added a static_dir url handler in you app.yaml file and have set you static directory (where you templates are) as a static_dir.

This makes your files unaccessible because static files are not available in the application's file system.

Elimine static_dir de app.yamlfile and add a static-templates folder in your project folder.

Create a jinja environment as follows:

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

Respondido 25 ago 12, 00:08

You can accept the answer if it worked, to help others who see the question - tikonom

Just to expand on this with something that seems obvious, but if a file is matched by a skip_files regex, you will also get the IOError. So, it can't be static and the path should not be skipped. - hjc1710

you have a mistake. 1. in your appengine config file, you need same code like that:

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

look that "- url items"... this is for your static content( js css img ) for your html template, you need a subfolder template son of your root-app, in my case is myapp/template and inside put your template( templates content, html )

your main app look like that. 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)

Respondido 19 Abr '14, 17:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.