英文:
golang: can't execute t.execute
问题
我正在尝试创建一个处理程序,每次从提交按钮获取数据时更新一行,以下是我的代码:
func RowHandler(res http.ResponseWriter, req *http.Request) {
    if req.Method != "POST" {
        http.ServeFile(res, req, "homepage.html")
        return
    }
    Person_id := req.FormValue("Person_id")
    stmt, err := db.Prepare("update Cityes set Status='right' where  Person_id=?")
    if err != nil {
        log.Print("error ", err)
    }
    _, err = stmt.Exec(&Person_id)
    t, err := template.ParseFiles("city_update.html") //这里我只想在HTML页面中显示一个文本
    if err != nil {
        log.Fatal(err)
    }
    err = t.Execute(res, "/city_update")
}
希望这对你有帮助!
英文:
I'm trying to make an Handler to update one row each time getting data from a submitt button,
here is my code:
func RowHandler(res http.ResponseWriter, req *http.Request) {
	if req.Method != "POST" {
		http.ServeFile(res, req, "homepage.html")
		return
	}
	Person_id := req.FormValue("Person_id")
	stmt, err := db.Prepare("update Cityes set Status='right' where  Person_id=?")
	if err != nil {
		log.Print("error ", err)
	}
	_, err = stmt.Exec(&Person_id)
	t, err := template.ParseFiles("city_update.html") //hier i just want to show a text in html Page
	if err != nil {
		log.Fatal(err)
	}
	err = t.Execute(res, "/city_update")
}
答案1
得分: 1
在这里,不要按照以下方式进行操作:
err = t.Execute(res, "/city_update")
而是将要用于填充模板的数据作为参数传递给Execute方法。文档链接
例如:
err = t.Execute(res, struct{ID string}{Person_id})
英文:
Here instead of following
err = t.Execute(res, "/city_update")
pass data to be used to fill your template as send arguement to Execute not string. link to doc
For example .
err = t.Execute(res,struct{ID string}{Person_id})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论