[英]Google App Engine yaml file configuration
I am having trouble using images, html files and javascript with Google App Engine.我在 Google App Engine 中使用图像、html 文件和 javascript 时遇到问题。 I have a folder in my application folder called static.
我的应用程序文件夹中有一个名为 static 的文件夹。 In the static folder I have 3 other folders called 'images', 'html', and 'javascript'.
在静态文件夹中,我还有 3 个其他文件夹,分别称为“图像”、“html”和“javascript”。 My handlers part of my app.yaml is as follows,
我的 app.yaml 的处理程序部分如下,
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,我使用 jinja2 渲染了一个 html,
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.我收到一个错误,提示找不到 jinja 模板,也找不到我的图像。 I thought this was the correct way to use upload and use files with Google App Engine but obviously I am wrong.
我认为这是使用 Google App Engine 上传和使用文件的正确方法,但显然我错了。
What is the proper way that I can use files in folders with Google App Engine?我可以通过 Google App Engine 使用文件夹中的文件的正确方法是什么?
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.如果您打算在 HTML 中使用模板系统,您不想将其声明为
static_dir
,原因是静态文件存储在与应用程序文件不同的文件系统中,因此它们将无法使成为。 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.当您使用
os.path.dirname(__file__).....
,您正在应用程序的文件系统中查找文件,这与您指定的static
目录不同。 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.
您的图像处理程序看起来不错,所以我会先尝试一下,看看它是否有所作为。
Using Jinja, you render a template which writes HTML.使用 Jinja,您可以渲染一个编写 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!!在您的 app.yaml 中,您使用: static/images 或 static/html 但在您的代码中,您使用了另一条路径!! Without the static folder.
没有静态文件夹。
So if you have a folder static and in that folder you have a folder image:因此,如果您有一个文件夹 static 并且在该文件夹中您有一个文件夹图像:
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')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.