go - 多次response.WriteHeader调用

标签 go go-templates

我是新手,渲染模板时遇到困难。

这是我用来生成模板的函数:

base.html

//Render templates for the given name, template definition and data object
func renderTemplate(w http.ResponseWriter, name string, template string, viewModel interface{}) {
    // Ensure the template exists in the map.
    tmpl, ok := templates[name]
    if !ok {
        http.Error(w, "The template does not exist.", http.StatusInternalServerError)
    }
    err := tmpl.ExecuteTemplate(w, template, viewModel)
    if err != nil {
    log.Printf("temlate error here")
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}


 func EditNote(w http.ResponseWriter, r *http.Request) {
    //var viewModel models.EditChnl
    vars := mux.Vars(r)
    //ch := bson.M{"id": "Ale"}
    title := vars["title"]
    log.Printf("%v\n", title)
    session, err := mgo.Dial("localhost")
    if err != nil {
      panic(err)
    }
        defer session.Close()
        session.SetMode(mgo.Monotonic, true)
        c := session.DB("tlgdb").C("chnls")
        log.Printf("title is %s \n", title)
        var result  []models.Chnl
        err = c.Find(bson.M{"title": "xxx"}).All(&result)
        log.Printf("%v\n", result)

        if err != nil {
          log.Printf("doc not found")
          log.Fatal(err)
          return
        }
    renderTemplate(w, "edit", "base", result)
}

这是模板:

{{define "base"}}
<html>
  <head>
    {{template "head" .}}
  </head>
  <body>

    {{template "body" .}}

  </body>
</html>
{{end}}

编辑.thml

{{define "head"}}<title>Edit Note</title>{{end}}
{{define "body"}}
<h1>Edit Note</h1>
<form action="/chnls/update/{{.Title}}" method="post">
<p>Title:<br> <input type="text" value="{{.Title}}" name="title"></p>
<p>Description:<br> <textarea rows="4" cols="50" name="description">{{.Description}}</textarea> </p>
<p><input type="submit" value="submit"/></p>
</form>
{{end}}

要渲染的对象是:

type Chnl struct {
    Id    bson.ObjectId `json:"id"  bson:"_id,omitempty"`
    Title       string
    Description string
    CreatedOn   time.Time
    Creator     string
    Visits  int
    Score       int
}

我尝试渲染的对象存在于 mongodb 中,我可以在控制台中打印出来:

[{ObjectIdHex("56cc4493bc54f4245cb4d36b") sometitle blabla 2016-02-23 12:37:55.972 +0100 CET blabla 0 0}]

但是我收到此错误:

temlate error here
http: multiple response.WriteHeader calls

我想知道这里出了什么问题以及如何解决它?

最佳答案

根本问题是您将 Chnl 的 slice 传递给模板:

var result  []models.Chnl
// ...
renderTemplate(w, "edit", "base", result)

并且 viewModelrenderTemplate() 参数值将是 result

在模板中,您可以引用点的字段,就像它是一个 Chnl 值而不是它的一部分一样:{{.Title}}。因此第一次尝试解决该问题将会失败。

记录错误很有用,因此更改日志记录以打印实际错误,而不仅仅是一般错误:

log.Printf("Temlate error here: %v", err)

结果是:

2016/02/24 14:57:09 Temlate error here: template: edit.html:4:30: executing "body" at <.Title>: can't evaluate field Title in type []main.Chnl

我认为您只想传递 1 个 Chnl 值,而不是其中的一部分。在 EditNote() 中:

if len(result) > 0 {
    renderTemplate(w, "edit", "base", result[0])
}

接下来,知道 http.Error()将内容写入响应。这意味着您无法向响应写入更多 header 值。通常,当您在处理程序中调用 http.Error() 时,您应该返回而不对响应执行任何操作:

if !ok {
    http.Error(w, "The template does not exist.", http.StatusInternalServerError)
    return // NOTE THIS RETURN
}

类似地,在所有 http.Error() 调用之后插入一个 return。您可以进行一些清理,但在 http.Error() 之后不应触及响应。

关于go - 多次response.WriteHeader调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35603131/

相关文章:

go - 在 Go 中按子数组值对 slice 进行分组

go - 如何使用 golang 和 labstack echo 处理对多个服务的代理

golang 不支持模板的结构 slice 深度

go - 如何将基本模板扩展到其他模板

heroku - 将 go webapps 部署到 heroku : can't find package issue

mongodb - 为什么mongo-go-driver聚合结果对象键返回 "Key"

go - 如何在 go 中使用模板进行排名

string - 如何在 go 模板中连接两个字符串?

go - 如何在 Go 中对模板(不是范围)进行循环?

go - 在 go 中,是否值得避免创建大小为 0 的 slice ?