英文:
What have I done wrong or how do I correct the output from my datastore key in html? datastore.Encode()?
问题
我相信我编程的能力和专业知识还处于初级阶段。我很抱歉...请随时提问,纠正我的风格、粗糙等等。我将从最后开始。我可能发布了太多的信息。
这是生成的HTML链接。我做错了什么或者错过了什么,以至于强制输出在我的数据存储键的前面加上“/Hobby”前缀?数字是正确的。我在管理面板中验证过了。
/hobby?action=admin&operation=edit&id=/Hobby,5222955109842944
预渲染的HTML
{{define "content"}}
<h2>{{.PageSetting.Title}}</h2>
<hr />
<a href="/hobby?action=admin&operation=add" class="btn"><i class="icon-pencil"></i>Add Hobby</a>
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Created</th>
<th>Updated</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
{{range .PageData.Hobby}}
<tr>
<td>{{.Title}}</td>
<td>{{.CreatedDate.Format "2006.01.02 @ 3:04pm"}}</td>
<td>{{.UpdatedDate.Format "2006.01.02 @ 3:04pm"}}</td>
<td>
<a href="/hobby?action=admin&operation=edit&id={{.Key}}" class="btn"><i class="icon-pencil"></i> Edit</a>
<a href="/hobby?action=admin&operation=delete&id={{.Key}}" class="btn"><i class="icon-trash"></i> Delete</a>
</td>
</tr>
{{end}}
</tbody>
</table>
<div class="pageination">
<ul class="pager">
{{if .PageSetting.ShowPrev}}
<li class="previous">
<a href="?action=admin&pid={{.PageSetting.PrevPageID}}">&larr; Older</a>
</li>
{{end}}
{{if .PageSetting.ShowNext}}
<li class="next">
<a href="?action=admin&pid={{.PageSetting.NextPageID}}">Newer &rarr;</a>
</li>
{{end}}
</ul>
</div>
{{end}}
生成HTML的函数
/*
* New Page
*
* @param layout (string)
* @param showSidebar (bool)
* @param pageData (*PageData)
*
* @return (*Page)
*/
func NewPage(pageSetting *PageSetting, pageData *PageData) *Page {
if pageSetting.Layout == "" {
pageSetting.Layout = DEFAULT_LAYOUT
}
if pageSetting.Title == "" {
pageSetting.Title = config.Title
}
if pageSetting.Description == "" {
pageSetting.Description = config.Description
}
return &Page{ PageSetting: pageSetting, PageData: pageData }
}
/*
* Render page
*
* @param pageFilePath (string)
* @param w (http.ResponseWriter)
*
* @return (error)
*/
func (page *Page) Render(pageFilePath string, w http.ResponseWriter) (err error) {
columnFilePath := page.PageSetting.Layout + ".html"
mainFilePath := "main.html"
contentFilePath := pageFilePath + ".html"
sidebarFilePath := "sidebar.html"
var tmpl *template.Template
switch page.PageSetting.ShowSidebar {
case true:
tmpl, err = template.ParseFiles(
LAYOUT_FOLDER + mainFilePath,
LAYOUT_FOLDER + columnFilePath,
LAYOUT_FOLDER + sidebarFilePath,
STATIC_FOLDER + contentFilePath)
case false:
tmpl, err = template.ParseFiles(
LAYOUT_FOLDER + mainFilePath,
LAYOUT_FOLDER + columnFilePath,
STATIC_FOLDER + contentFilePath)
}
if err != nil {
return
}
tmpl.Execute(w, page)
return
}
生成“Hobby”的函数
type HobbyDB struct {
Key *datastore.Key `datastore:"-"`
Title string
Description []byte
CreatedDate time.Time
UpdatedDate time.Time
}
type HobbyData struct {
Key *datastore.Key `datastore:"-"`
Title string
Description string
CreatedDate time.Time
UpdatedDate time.Time
}
func getHobbyData(dbQuery *datastore.Query, MDOutput bool, c appengine.Context) (hobbyData []HobbyData , err error) {
var h []*HobbyDB
k, err := dbQuery.GetAll(c, &h)
if err != nil {
return
}
hobbyData = make([]HobbyData, len(h))
for i := range h {
hobbyData[i].Key = k[i]
hobbyData[i].Title = h[i].Title
if MDOutput {
hobbyData[i].Description = string(blackfriday.MarkdownCommon(h[i].Description))
} else {
hobbyData[i].Description = string(h[i].Description)
}
hobbyData[i].CreatedDate = h[i].CreatedDate
hobbyData[i].UpdatedDate = h[i].UpdatedDate
}
return
}
func hobbyList(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// 获取爱好数据
// 获取页面ID
pageId, _ := strconv.Atoi(getUrlQuery(r.URL, "pid"))
pageSize := 10
// 获取偏移量和页面数
offset, pageNums := getOffset("Hobby", pageId, pageSize, c)
// 新建PageSetting
pageSetting := new(PageSetting)
// 设置PageSetting
pageSetting.Title = "Hobby Manager - " + config.Title
pageSetting.Layout = "column1"
// 显示上一页和下一页按钮
if pageId <= 0 || pageId > pageNums {
pageId = 1
}
if pageId < pageNums {
pageSetting.ShowPrev = true
}
if pageId != 1 {
pageSetting.ShowNext = true
}
pageSetting.PrevPageID = pageId + 1
pageSetting.NextPageID = pageId - 1
// 获取爱好数据
dbQuery := datastore.NewQuery("Hobby").Order("-UpdatedDate").Offset(offset).Limit(pageSize)
hobbyData, err := getHobbyData(dbQuery, false, c)
if err != nil {
serveError(c, w, err)
return
}
// 新建PageData
pageData := &PageData{ Hobby: hobbyData }
// 新建Page
page := NewPage(pageSetting, pageData)
// 渲染页面
page.Render("hobby/admin", w)
}
英文:
I believe my ability and expertise to program is in its infancy. I apologize ... Please do not hesitate to ask questions, correct my style, crudeness, etc. I am going to start from the end. I might have posted too much information.
This is the generated html link. What have I done or missed to force the output to prefix "/Hobby," on the front of my datastore key? The number is correct. I have verified this in the admin panel.
/hobby?action=admin&operation=edit&id=/Hobby,5222955109842944
Pre-rendered html
{{define "content"}}
<h2>{{.PageSetting.Title}}</h2>
<hr />
<a href="/hobby?action=admin&operation=add" class="btn"><i class="icon-pencil"></i>Add Hobby</a>
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Created</th>
<th>Updated</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
{{range .PageData.Hobby}}
<tr>
<td>{{.Title}}</td>
<td>{{.CreatedDate.Format "2006.01.02 @ 3:04pm"}}</td>
<td>{{.UpdatedDate.Format "2006.01.02 @ 3:04pm"}}</td>
<td>
<a href="/hobby?action=admin&operation=edit&id={{.Key}}" class="btn"><i class="icon-pencil"></i> Edit</a>
<a href="/hobby?action=admin&operation=delete&id={{.Key}}" class="btn"><i class="icon-trash"></i> Delete</a>
</td>
</tr>
{{end}}
</tbody>
</table>
<div class="pageination">
<ul class="pager">
{{if .PageSetting.ShowPrev}}
<li class="previous">
<a href="?action=admin&pid={{.PageSetting.PrevPageID}}">&larr; Older</a>
</li>
{{end}}
{{if .PageSetting.ShowNext}}
<li class="next">
<a href="?action=admin&pid={{.PageSetting.NextPageID}}">Newer &rarr;</a>
</li>
{{end}}
</ul>
</div>
{{end}}
Functions that generate html
/*
* New Page
*
* @param layout (string)
* @param showSidebar (bool)
* @param pageData (*PageData)
*
* @return (*Page)
*/
func NewPage(pageSetting *PageSetting, pageData *PageData) *Page {
if pageSetting.Layout == "" {
pageSetting.Layout = DEFAULT_LAYOUT
}
if pageSetting.Title == "" {
pageSetting.Title = config.Title
}
if pageSetting.Description == "" {
pageSetting.Description = config.Description
}
return &Page{ PageSetting: pageSetting, PageData: pageData }
}
/*
* Render page
*
* @param pageFilePath (string)
* @param w (http.ResponseWriter)
*
* @return (error)
*/
func (page *Page) Render(pageFilePath string, w http.ResponseWriter) (err error) {
columnFilePath := page.PageSetting.Layout + ".html"
mainFilePath := "main.html"
contentFilePath := pageFilePath + ".html"
sidebarFilePath := "sidebar.html"
var tmpl *template.Template
switch page.PageSetting.ShowSidebar {
case true:
tmpl, err = template.ParseFiles(
LAYOUT_FOLDER + mainFilePath,
LAYOUT_FOLDER + columnFilePath,
LAYOUT_FOLDER + sidebarFilePath,
STATIC_FOLDER + contentFilePath)
case false:
tmpl, err = template.ParseFiles(
LAYOUT_FOLDER + mainFilePath,
LAYOUT_FOLDER + columnFilePath,
STATIC_FOLDER + contentFilePath)
}
if err != nil {
return
}
tmpl.Execute(w, page)
return
}
Functions to generate 'Hobby'
type HobbyDB struct {
Key *datastore.Key `datastore:"-"`
Title string
Description []byte
CreatedDate time.Time
UpdatedDate time.Time
}
type HobbyData struct {
Key *datastore.Key `datastore:"-"`
Title string
Description string
CreatedDate time.Time
UpdatedDate time.Time
}
func getHobbyData(dbQuery *datastore.Query, MDOutput bool, c appengine.Context) (hobbyData []HobbyData , err error) {
var h []*HobbyDB
k, err := dbQuery.GetAll(c, &h)
if err != nil {
return
}
hobbyData = make([]HobbyData, len(h))
for i := range h {
hobbyData[i].Key = k[i]
hobbyData[i].Title = h[i].Title
if MDOutput {
hobbyData[i].Description = string(blackfriday.MarkdownCommon(h[i].Description))
} else {
hobbyData[i].Description = string(h[i].Description)
}
hobbyData[i].CreatedDate = h[i].CreatedDate
hobbyData[i].UpdatedDate = h[i].UpdatedDate
}
return
}
func hobbyList(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// Get hobby data
// Get page id
pageId, _ := strconv.Atoi(getUrlQuery(r.URL, "pid"))
pageSize := 10
// Get offset and page numbers
offset, pageNums := getOffset("Hobby", pageId, pageSize, c)
// New PageSetting
pageSetting := new(PageSetting)
// Setting PageSetting
pageSetting.Title = "Hobby Manager - " + config.Title
pageSetting.Layout = "column1"
// showNext and showPrev button
if pageId <= 0 || pageId > pageNums {
pageId = 1
}
if pageId < pageNums {
pageSetting.ShowPrev = true
}
if pageId != 1 {
pageSetting.ShowNext = true
}
pageSetting.PrevPageID = pageId + 1
pageSetting.NextPageID = pageId - 1
// Get hobby data
dbQuery := datastore.NewQuery("Hobby").Order("-UpdatedDate").Offset(offset).Limit(pageSize)
hobbyData, err := getHobbyData(dbQuery, false, c)
if err != nil {
serveError(c, w, err)
return
}
// New PageData
pageData := &PageData{ Hobby: hobbyData }
// New Page
page := NewPage(pageSetting, pageData)
// Render page
page.Render("hobby/admin", w)
}
答案1
得分: 1
将.Key
更改为.Key.IntID
:
<a href="/hobby?action=admin&operation=edit&id={{.Key.IntID}}">
文档:http://godoc.org/code.google.com/p/appengine-go/appengine/datastore#Key.IntID
英文:
Change .Key
to .Key.IntID
:
<a href="/hobby?action=admin&operation=edit&id={{.Key.IntID}}">
Docs: http://godoc.org/code.google.com/p/appengine-go/appengine/datastore#Key.IntID
答案2
得分: 0
使用.Key.IntID
,就像mjibson建议的那样,或者使用.Key.Encode
来获取完整的编码键,如果您在键中使用祖先,则这是更好的选择,因为在这种情况下,整数ID不能保证是唯一的。
英文:
Use .Key.IntID
like mjibson suggested, or use .Key.Encode
to get the full encoded key, which is a better option if you use ancestors with your keys since the integer ID is not guaranteed to be unique in that case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论