【问题标题】:why even after adding the -url: /images in app.yaml, i can't access /images/med-9.png ,为什么即使在 app.yaml 中添加 -url: /images 后,我也无法访问 /images/med-9.png ,
【发布时间】:2015-12-30 14:36:33
【问题描述】:

我正在学习 python,特别是使用 python 进行 Web 开发,并且正在学习 udacity 中的 cs253 课程,现在在执行第 4 单元练习之后,我想在我的 html 模板中添加一个图像,我创建了一个目录:“模板” 在我的“hellowebappworld”目录中,该目录是由应用程序引擎和模板内部托管的目录,我创建了一个文件夹:“images”,其中有我的图像。即使添加了 url : / images 和 static_dir: images m 也无法访问图像

这是我的 app.yaml:

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>

【问题讨论】:

    标签: python image jinja2


    【解决方案1】:

    尝试将您的 app.yaml 文件修改为此..

    - url: /templates/images
      static_dir: templates/images 
    

    顺便说一句,我喜欢这样构造我的静态文件..

    - url: /static
      static_dir: static
    

    然后我的静态文件夹位于我的应用引擎应用程序的根目录中。

    • 桌面
      • 项目文件夹
        • 静态
          • 图片
          • js
          • CSS
        • 模板
        • main.py
        • app.yaml

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多