将*datastore.Key添加到切片中

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

Add *datastore.Key to slice

问题

我成功地获取了所有的数据,并使用模板在表格中显示出来。我在这段代码中使用Go语言查询数据存储,实体类型为Membership。

在HTML页面中,所有的数据都被显示出来,但是键值没有显示。

我尝试将Key *datastore.Key作为Membership结构体的属性添加进去,但是仍然没有成功。

以下是我的代码:

package hello

import (
    "appengine"
    "appengine/datastore"
    "html/template"
    "net/http"
    "time"
)        

type Membership struct {
    Key *datastore.Key
    Author  string
    Content string
    FirstName string
    LastName string
    Address string
    Email string 
    Grade string
    Date    time.Time
}

func init() {
    http.HandleFunc("/", members)
}

func members(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    q := datastore.NewQuery("Membership").Order("-Date")
    memberships := make([]Membership, 0, 10)
    if _, err := q.GetAll(c, &memberships); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    if err := membersTemplate.Execute(w, memberships); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

var membersTemplate = template.Must(template.New("page").Parse(membersTemplateHTML))

const membersTemplateHTML = `
<html>
  <body>
    <table><tr><th>Key</th><th>Name</th><th>Email</th><th>Adress</th><th>Grade</th></tr>
      {{range .}}
        <tr>
          <td>{{.Key}}</td>
          <td>{{.FirstName}} {{.LastName}}</td>
          <td>{{.Email}}</td>
          <td>{{.Address}}</td>
          <td>{{.Grade}}</td>
        </tr>
      {{end}}
    </table>
  </body>
</html>`
英文:

I am getting all the data successfully and displayed in a table using template. I am using Go in this code querying the datastore with Membership as entity type.

In the html page, all the data are displayed but not the keys.

I have tried to add Key *datastore.Key as property to the Membership struct but still no luck.

Here is my code:

package hello

import (
    &quot;appengine&quot;
    &quot;appengine/datastore&quot;
    &quot;html/template&quot;
    &quot;net/http&quot;
    &quot;time&quot;
)        

type Membership struct {
    Key *datastore.Key
    Author  string
    Content string
    FirstName string
    LastName string
    Address string
    Email string 
    Grade string
    Date    time.Time
}

func init() {
    http.HandleFunc(&quot;/&quot;, members)
}

func members(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    q := datastore.NewQuery(&quot;Membership&quot;).Order(&quot;-Date&quot;)
    memberships := make([]Membership, 0, 10)
    if _, err := q.GetAll(c, &amp;memberships); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    if err := membersTemplate.Execute(w, memberships); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

var membersTemplate = template.Must(template.New(&quot;page&quot;).Parse(membersTemplateHTML))

const membersTemplateHTML = `
&lt;html&gt;
  &lt;body&gt;
    &lt;table&gt;&lt;tr&gt;&lt;th&gt;Key&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Email&lt;/th&gt;&lt;th&gt;Adress&lt;/th&gt;&lt;th&gt;Grade&lt;/th&gt;&lt;/tr&gt;
      {{range .}}
        &lt;tr&gt;
          &lt;td&gt;{{.Key}}&lt;/td&gt;
          &lt;td&gt;{{.FirstName}} {{.LastName}}&lt;/td&gt;
          &lt;td&gt;{{.Email}}&lt;/td&gt;
          &lt;td&gt;{{.Address}}&lt;/td&gt;
          &lt;td&gt;{{.Grade}}&lt;/td&gt;
        &lt;/tr&gt;
      {{end}}
    &lt;/table&gt;
  &lt;/body&gt;
&lt;/html&gt;`

答案1

得分: 3

你需要将键分配给Key属性:

func members(w http.ResponseWriter, r *http.Request) {
  ....
    if keys, err := q.GetAll(c, &memberships); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    // 遍历成员并添加键
    for i := range memberships {
         memberships[i].Key = keys[i]
    }
  ....
}

你可能还想禁用在数据存储中存储Key属性:

type Membership struct {
    Key *datastore.Key `datastore:"-"`
    ....
}

顺便说一下,如果你想在HTML中显示*datastore.Key,你应该在显示之前对其进行Encode()处理。

英文:

You need to assign the key to the Key property:

func members(w http.ResponseWriter, r *http.Request) {
  ....
    if keys, err := q.GetAll(c, &amp;memberships); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    // Loop over the membership and add the keys
    for i := range memberships {
         memberships[i].Key = keys[i]
    }
  ....
}

You might also want to disable storing the Key property in the datastore:

type Membership struct {
    Key *datastore.Key `datastore:&quot;-&quot;`
    ....
}

Btw. if you want to display the *datastore.Key in HTML, you should Encode() it before.

huangapple
  • 本文由 发表于 2013年1月5日 04:44:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/14164678.html
匿名

发表评论

匿名网友

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

确定