gpt4 book ai didi

html - 浏览器不解释 html 模板

转载 作者:IT王子 更新时间:2023-10-29 01:59:54 25 4
gpt4 key购买 nike

我在一个非常小的 Go 应用程序中有一个函数,它查询数据库,检索一些数据并将其插入模板 (main.html)。数据被插入到模板中(见图),但是浏览器(Chrome 和 Firefox)不解释 html enter image description here .我的浏览器在其他方面运行良好。有没有我对模板做的不正确?

func Root(w http.ResponseWriter, r *http.Request) {

t := template.Must(template.New("main").ParseFiles("main.html"))
rows, err := db.Query("SELECT title, author, description FROM books")
PanicIf(err)
defer rows.Close()

books := []Book{}

for rows.Next() {
b := Book{}
err := rows.Scan(&b.Title, &b.Author, &b.Description)
PanicIf(err)
books = append(books, b)
}
t.ExecuteTemplate(w, "main.html", books)

}

main.html

<html>
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
</head>


<body>

<div class="container">
<table class="table">
<tr>
<th>Title</th>
<th>Author</th>
<th>Description</th>
</tr>
{{ range . }}
<tr>
<td>{{.Title}}</td>
<td>{{.Author}}</td>
<td>{{.Description}}</td>
</tr>
{{ end }}
</table>
</div>
</body>


</html>

最佳答案

很可能您正在执行 import "text/template" 并且应该执行 import "html/template",但其他答案也会为您修复它。

关于html - 浏览器不解释 html 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25313365/

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