gpt4 book ai didi

google-app-engine - Google App Engine,Jinja2 CSS 样式表未加载 (404)

转载 作者:行者123 更新时间:2023-12-02 04:36:50 25 4
gpt4 key购买 nike

长期倾听者,第一次来访者。

我在使用 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

这是我的 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

这是我的 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()

这是我的 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>

我通过 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

在这种情况下,/stylesheets 模式将在 /.* 模式匹配之前匹配到适当的路径。有关 URL 映射和您可以在 app.yaml 中指定的其他选项的更多信息,请参阅 app.yaml reference .

关于google-app-engine - Google App Engine,Jinja2 CSS 样式表未加载 (404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com