golang: 无法执行 t.execute

huangapple go评论76阅读模式
英文:

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})

huangapple
  • 本文由 发表于 2017年7月31日 15:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/45410176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定