英文:
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 (
"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>`
答案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, &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:"-"`
....
}
Btw. if you want to display the *datastore.Key
in HTML, you should Encode()
it before.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论