Python 为什么即使在app.yaml中添加了-url:/images,我也能';t access/images/med-9.png,
我正在学习python,特别是使用python进行web开发,并且正在学习udacity中的cs253课程,现在在完成第4单元练习后,我想在我的html模板中添加一个图像,我在我的“HellowBappWorld”目录中创建了一个目录:“Templates”,该目录由app engine托管并位于模板内部,我已经做了一个文件夹:“图像”,在那里我有我的图像。即使在添加url:/images和static\u dir:images之后,我也无法访问该图像 这是我的应用程序。yaml:Python 为什么即使在app.yaml中添加了-url:/images,我也能';t access/images/med-9.png,,python,image,jinja2,Python,Image,Jinja2,我正在学习python,特别是使用python进行web开发,并且正在学习udacity中的cs253课程,现在在完成第4单元练习后,我想在我的html模板中添加一个图像,我在我的“HellowBappWorld”目录中创建了一个目录:“Templates”,该目录由app engine托管并位于模板内部,我已经做了一个文件夹:“图像”,在那里我有我的图像。即使在添加url:/images和static\u dir:images之后,我也无法访问该图像 这是我的应用程序。yaml: applic
application: hellowebappworld
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /images
static_dir: images
- url: .*
script: main.py
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
以下是main.py:
import os
import webapp2
import jinja2
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)
class Art(db.Model):
title = db.StringProperty(required = True)
art = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class MainPage(Handler):
def render_front(self, title="", art="", error=""):
arts = db.GqlQuery("SELECT * FROM Art ORDER BY created DESC")
self.render("front.html", title=title, art=art, error = error, arts = arts)
def get(self):
self.render_front()
def post(self):
title = self.request.get("title")
art = self.request.get("art")
if title and art:
a = Art(title = title, art = art)
a.put()
self.redirect("/")
else:
error = "we need both a title and some artwork!"
self.render_front(title,art,error)
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
最后是我的模板:
<!DOCTYPE html>
<html>
<head>
<title>/ascii/</title>
</head>
<body>
<form method="post">
<label>
<div>title</div><input type="text" name="title" value= {{title}}>
</label>
<label>
<div>art</div><textarea name="art"> {{art}} </textarea>
</label>
<div class="error"> {{error}}</div>
<input type="submit">
</form>
<hr>
{% for art in arts %}
<div class="art">
<div class="art-title">{{art.title}}</div>
<pre class="art-body">{{art.art}}</pre>
</div>
{% endfor %}
<img src="/images/med-9.png">
</body>
/ascii码/
标题
艺术{{art}
{{error}}
{艺术中的艺术%}
{{第条标题}
{{art.art}
{%endfor%}
尝试将app.yaml文件修改为此
- url: /templates/images
static_dir: templates/images
另一方面,我喜欢这样构造静态文件
- url: /static
static_dir: static
然后,我的静态文件夹位于我的应用程序引擎应用程序的根目录中
- 桌面
- 项目文件夹
- 静止的
- 图像
- js
- css
- 模板
- main.py
- app.yaml
- 静止的
- 项目文件夹