Mag*_*eno 2 google-app-engine yaml jinja2 python-2.7 webapp2
长时间听众,第一次来电.
我对Google App Engine,Jinja2和CSS有一些令人沮丧,看似无法解释的问题.
我的模板正在运行,我的应用程序的功能正常工作(用户,博客帖子等),但CSS文件在我的Chrome调试工具和我的Google App Engine日志中显示了一个很大的404.为什么我的/stylesheets/main.css没有加载?
亲爱的互联网,我很想听到这只是一个错字.我敢肯定我只是个白痴.
这是我的文件目录:
stylesheets
main.css
templates
base.html
blog.html
front.html
login.html
newpost.html
signup.html
welcome.html
app.yaml
blogs.py
favicon.ico
index.yaml
main.py
users.py
utilities.py
Run Code Online (Sandbox Code Playgroud)
这是我的YAML文件:
application: hello-udacity-5681
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /.*
script: main.app
- url: /stylesheets
static_dir: stylesheets
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
Run Code Online (Sandbox Code Playgroud)
这是我的main.py:
import webapp2
import os
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 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 MainHandler(Handler):
def render_front(self):
self.render("base.html")
def get(self):
self.render_front()
Run Code Online (Sandbox Code Playgroud)
这是我的base.html:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css"/>
<title>Blog</title>
</head>
<body>
<a href="/" class="main-title">Blog</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我通过http://jigsaw.w3.org/css-validator/运行我的main.css 没有任何问题,所以我不会厌烦你.
为什么我仍然为我的/stylesheets/main.css获得404?
你的app.yaml处理程序部分应该是这样的
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /stylesheets
static_dir: stylesheets
- url: /.*
script: main.app
Run Code Online (Sandbox Code Playgroud)
在这种情况下,/stylesheets模式将在/.*模式用于适当路径之前匹配.有关URL映射和您可以在app.yaml中指定的其他选项的更多信息,请参阅app.yaml参考.
| 归档时间: |
|
| 查看次数: |
2504 次 |
| 最近记录: |