golang: 无法执行 t.execute

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

golang: can't execute t.execute

问题

我正在尝试创建一个处理程序,每次从提交按钮获取数据时更新一行,以下是我的代码:

  1. func RowHandler(res http.ResponseWriter, req *http.Request) {
  2. if req.Method != "POST" {
  3. http.ServeFile(res, req, "homepage.html")
  4. return
  5. }
  6. Person_id := req.FormValue("Person_id")
  7. stmt, err := db.Prepare("update Cityes set Status='right' where Person_id=?")
  8. if err != nil {
  9. log.Print("error ", err)
  10. }
  11. _, err = stmt.Exec(&Person_id)
  12. t, err := template.ParseFiles("city_update.html") //这里我只想在HTML页面中显示一个文本
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. err = t.Execute(res, "/city_update")
  17. }

希望这对你有帮助!

英文:

I'm trying to make an Handler to update one row each time getting data from a submitt button,
here is my code:

  1. func RowHandler(res http.ResponseWriter, req *http.Request) {
  2. if req.Method != "POST" {
  3. http.ServeFile(res, req, "homepage.html")
  4. return
  5. }
  6. Person_id := req.FormValue("Person_id")
  7. stmt, err := db.Prepare("update Cityes set Status='right' where Person_id=?")
  8. if err != nil {
  9. log.Print("error ", err)
  10. }
  11. _, err = stmt.Exec(&Person_id)
  12. t, err := template.ParseFiles("city_update.html") //hier i just want to show a text in html Page
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. err = t.Execute(res, "/city_update")
  17. }

答案1

得分: 1

在这里,不要按照以下方式进行操作:

  1. err = t.Execute(res, "/city_update")

而是将要用于填充模板的数据作为参数传递给Execute方法。文档链接

例如:

  1. err = t.Execute(res, struct{ID string}{Person_id})
英文:

Here instead of following

  1. 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 .

  1. 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:

确定