Jake Runzer
Jake Runzer

Reputation: 1007

Google App Engine yaml file configuration

I am having trouble using images, html files and javascript with Google App Engine. I have a folder in my application folder called static. In the static folder I have 3 other folders called 'images', 'html', and 'javascript'. My handlers part of my app.yaml is as follows,

handlers:
- url: /html
  static_dir: static/html

- url: /javascript
  static_dir: static/javascript

- url: /images
  static_dir: static/images

- url: /.*
  script: index.app

I try to access an image with,

self.response.out.write('<img src="images/image.png">')

and I render an html using jinja2 with,

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

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

I am getting an error that the template for jinja could not be found and my image is also not found. I thought this was the correct way to use upload and use files with Google App Engine but obviously I am wrong.

What is the proper way that I can use files in folders with Google App Engine?

Upvotes: 1

Views: 1594

Answers (2)

voscausa
voscausa

Reputation: 11706

Using Jinja, you render a template which writes HTML. That is not static serving.

And in your app.yaml you use: static/images or static/html But in your code you use another path!! Without the static folder.

So if you have a folder static and in that folder you have a folder image:

self.response.out.write('<img src="/images/image.png">')

And your loader:

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

Upvotes: 1

RocketDonkey
RocketDonkey

Reputation: 37249

If you are going to be using a templating system with your HTML, you don't want to declare it as a static_dir, the reason being that static files are stored in a separate filesystem than your application files, so they will not be able to render. When you use os.path.dirname(__file__)....., you are looking for file's in your application's filesystem, which is distinct from the static dir you have specified. So the first thing to try is just removing:

- url: /html
  static_dir: static/html

from your handlers. This will then look for your application filesystem and (hopefully :) ) find it. Your image handler looks fine, so I would try that first and see if it makes a difference.

Upvotes: 1

Related Questions