英文:
Image didnt show writing on html with img on golang
问题
图片在编写HTML时没有显示出来。
我可以在main.go(parsefile)上显示图片,但在HTML上编写时图片没有显示出来。
我尝试了html/template,但也没有起作用。
代码:_base.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ template "title" . }}</title>
</head>
<body>
<section id="contents">
{{ template "content" . }}
</section>
<footer id="footer">
My Cool Guestbook 2000 © me forever
</footer>
</body>
</html>
代码:(index.html)
{{ define "title" }}Guestbook{{ end }}
{{ define "content" }}
<h1>Guestbook</h1>
<p>Hello, and welcome to my guestbook, because it's 1997!</p>
<ul class="guests">
<li>...</li>
</ul>
<img src="image.png">
{{ end }}
代码:(main.go的某个部分)
var index = template.Must(template.ParseFiles(
"templates/_base.html",
"templates/index.html",
))
func hello(w http.ResponseWriter, req *http.Request) {
if err := index.Execute(w, nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main(){
// 网络应用程序正常工作,但图片没有显示
// ...
}
请注意,这只是代码的一部分,可能还有其他原因导致图片无法显示。你可能需要检查图片文件的路径是否正确,并确保服务器能够正确地提供该图片文件。
英文:
image didnt show when writing on html.
i can display only image writing on main.go (parsefile).
but writing on html image didnt show.
i tried html/template. it didnt work too.
code: _base.html
<!DOCTYPE html>
<html lang="en">`enter code here`
<head>
<title>{{ template "title" . }}</title>
</head>
<body>
<section id="contents">
{{ template "content" . }}
</section>
<footer id="footer">
My Cool Guestbook 2000 © me forever
</footer>
</body>
</html>
code:(index.html)
{{ define "title" }}Guestbook{{ end }}
{{ define "content" }}
<h1>Guestbook</h1>
<p>Hello, and welcome to my guestbook, because it's 1997!</p>
<ul class="guests">
<li>...</li>
</ul>
<img src="image.png">
{{ end }}
code:(main.go some part)
var index = template.Must(template.ParseFiles(
"templates/_base.html",
"templates/index.html",
))
func hello(w http.ResponseWriter, req *http.Request) {
if err := index.Execute(w, nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main(){ etc...
web application work fine. but image didnt show
答案1
得分: 3
打开你的网络浏览器,查看该图像资源的请求返回了什么。
很可能返回的是404 Not Found
,因为无法找到该文件。
例如,在Chrome浏览器中,可以这样查看:
英文:
Open your web browser and see what's the request to that image resource is returning.
It's probably returning a 404 Not Found
because it can't locate the file.
For example in Chrome that would be:
答案2
得分: 1
添加到Agis的回答中:
也许你只是注册了用于渲染模板的处理程序,而没有提供像图片这样的文件。
你可以使用http.FileServer
来提供你的图片。
英文:
Adding to Agis's answer:
Maybe you are registered handlers just for rendering templates, not for providing files such as image.
You can use http.FileServer
to provide your images.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论