What have I done wrong or how do I correct the output from my datastore key in html? datastore.Encode()?

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

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}}">← Older</a>
        </li>
    {{end}}
    {{if .PageSetting.ShowNext}}
        <li class="next">
            <a href="?action=admin&pid={{.PageSetting.NextPageID}}">Newer →</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&amp;operation=edit&amp;id=/Hobby,5222955109842944

Pre-rendered html

{{define &quot;content&quot;}}
&lt;h2&gt;{{.PageSetting.Title}}&lt;/h2&gt;
&lt;hr /&gt;
&lt;a href=&quot;/hobby?action=admin&amp;operation=add&quot; class=&quot;btn&quot;&gt;&lt;i class=&quot;icon-pencil&quot;&gt;&lt;/i&gt;Add Hobby&lt;/a&gt;
&lt;table class=&quot;table table-striped&quot;&gt;
&lt;thead&gt;
    &lt;tr&gt;
        &lt;th&gt;Title&lt;/th&gt;
        &lt;th&gt;Created&lt;/th&gt;
        &lt;th&gt;Updated&lt;/th&gt;
        &lt;th&gt;Operations&lt;/th&gt;
    &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{{range .PageData.Hobby}}
    &lt;tr&gt;
        &lt;td&gt;{{.Title}}&lt;/td&gt;
        &lt;td&gt;{{.CreatedDate.Format &quot;2006.01.02 @ 3:04pm&quot;}}&lt;/td&gt;
        &lt;td&gt;{{.UpdatedDate.Format &quot;2006.01.02 @ 3:04pm&quot;}}&lt;/td&gt;
        &lt;td&gt;
            &lt;a href=&quot;/hobby?action=admin&amp;operation=edit&amp;id={{.Key}}&quot; class=&quot;btn&quot;&gt;&lt;i class=&quot;icon-pencil&quot;&gt;&lt;/i&gt; Edit&lt;/a&gt;
            &lt;a href=&quot;/hobby?action=admin&amp;operation=delete&amp;id={{.Key}}&quot; class=&quot;btn&quot;&gt;&lt;i class=&quot;icon-trash&quot;&gt;&lt;/i&gt; Delete&lt;/a&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
{{end}}
&lt;/tbody&gt;
&lt;/table&gt;
&lt;div class=&quot;pageination&quot;&gt;
    &lt;ul class=&quot;pager&quot;&gt;
    {{if .PageSetting.ShowPrev}}
        &lt;li class=&quot;previous&quot;&gt;
            &lt;a href=&quot;?action=admin&amp;pid={{.PageSetting.PrevPageID}}&quot;&gt;&amp;larr; Older&lt;/a&gt;
        &lt;/li&gt;
    {{end}}
    {{if .PageSetting.ShowNext}}
        &lt;li class=&quot;next&quot;&gt;
            &lt;a href=&quot;?action=admin&amp;pid={{.PageSetting.NextPageID}}&quot;&gt;Newer &amp;rarr;&lt;/a&gt;
        &lt;/li&gt;
    {{end}}
    &lt;/ul&gt;
&lt;/div&gt;
{{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 == &quot;&quot; {
        pageSetting.Layout = DEFAULT_LAYOUT
    }

    if pageSetting.Title == &quot;&quot; {
        pageSetting.Title = config.Title
    }

    if pageSetting.Description == &quot;&quot; {
        pageSetting.Description = config.Description
    }

    return &amp;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 + &quot;.html&quot;
    mainFilePath    := &quot;main.html&quot;
    contentFilePath := pageFilePath + &quot;.html&quot;
    sidebarFilePath := &quot;sidebar.html&quot;

    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:&quot;-&quot;`
	Title string
	Description []byte
	CreatedDate time.Time
	UpdatedDate time.Time
}

type HobbyData struct {
    Key *datastore.Key `datastore:&quot;-&quot;`
    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, &amp;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, &quot;pid&quot;))
    pageSize  := 10

    // Get offset and page numbers
    offset, pageNums := getOffset(&quot;Hobby&quot;, pageId, pageSize, c)

    // New PageSetting
    pageSetting := new(PageSetting)

    // Setting PageSetting
    pageSetting.Title  = &quot;Hobby Manager - &quot; + config.Title
    pageSetting.Layout = &quot;column1&quot;

    // showNext and showPrev button
    if pageId &lt;= 0 || pageId &gt; pageNums {
        pageId = 1
    }
    if pageId &lt; pageNums {
        pageSetting.ShowPrev = true
    }
    if pageId != 1 {
        pageSetting.ShowNext = true
    }
    pageSetting.PrevPageID = pageId + 1
    pageSetting.NextPageID = pageId - 1

    // Get hobby data
    dbQuery := datastore.NewQuery(&quot;Hobby&quot;).Order(&quot;-UpdatedDate&quot;).Offset(offset).Limit(pageSize)
    hobbyData, err := getHobbyData(dbQuery, false, c)
    if err != nil {
        serveError(c, w, err)
        return
    }

    // New PageData
    pageData := &amp;PageData{ Hobby: hobbyData }

    // New Page
    page := NewPage(pageSetting, pageData)

    // Render page
    page.Render(&quot;hobby/admin&quot;, w)
}

答案1

得分: 1

.Key更改为.Key.IntID

&lt;a href=&quot;/hobby?action=admin&amp;operation=edit&amp;id={{.Key.IntID}}&quot;&gt;

文档:http://godoc.org/code.google.com/p/appengine-go/appengine/datastore#Key.IntID

英文:

Change .Key to .Key.IntID:

&lt;a href=&quot;/hobby?action=admin&amp;operation=edit&amp;id={{.Key.IntID}}&quot;&gt;

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.

huangapple
  • 本文由 发表于 2013年6月8日 18:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/16998427.html
匿名

发表评论

匿名网友

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

确定