英文:
How to use externel CSS and (static) images in Go, Google App Engine
问题
我正在使用Go编写一个网页。Go代码使用“html/template”解析HTML。我想在项目中使用CSS。当我使用内部CSS代码时,一切都正常,但是当我想要切换到外部CSS时,它不起作用。看起来它无法访问.css文件。
这是我的app.yaml配置:
application: makerboardstest
version: 1
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
- url: /stylesheets
static_dir: stylesheets
- url: /images
static_dir: images
这是我想要从HTML中访问.css的方式:
<head>
<link href="/stylesheets/main.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
我还遇到了(静态)图像的问题。我想使用以下HTML代码访问图像:
<img src="/images/img1.jpg" />
问题可能是什么?
(我在我的PC上进行测试,Win 7)
英文:
I'm working on a webpage in Go. The Go code uses "html/template" to parse HTML. I would like to use CSS in the project. Everything is working well when I use internal CSS code, but when I would like to change to external it dosen't work. It looks like it can't access to the .css file.
Here is my app.yaml configuration:
application: makerboardstest
version: 1
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
- url: /stylesheets
static_dir: stylesheets
- url: /images
static_dir: images
Here is how I would like to access to the .css from the html:
<head>
<link href="/stylesheets/main.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
I also have problems with (static) images. I would like to access to the image with this html code:
<img src="/images/img1.jpg" />
What can be the problem?
(I'm testing it on my PC, Win 7)
答案1
得分: 4
处理程序按顺序进行检查,您的第一个处理程序与所有内容匹配;将静态处理程序移到其上方。换句话说,当您的浏览器请求/stylesheets/main.css
时,它会与第一个处理程序的/.*
模式匹配,并要求go来提供它,而不是尝试静态目录。如果您交换处理程序的顺序,它将首先匹配/stylesheets
并从静态目录中提供它。
例如:
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /images
static_dir: images
- url: /.*
script: _go_app
英文:
The handlers are checked in order, and your first handler matches everything; move your static handlers up above it. In other words, when your browser makes a request for /stylesheets/main.css
, it matches the /.*
pattern of your first handler and asks go to serve it rather than trying the static dir. If you flip the order of the handlers, it will match /stylesheets
first and serve it from the static dir.
i.e.:
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /images
static_dir: images
- url: /.*
script: _go_app
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论