1

我相信我的编程能力和专业知识还处于起步阶段。我道歉......请不要犹豫,提出问题,纠正我的风格,粗鲁等等。我将从头开始。我可能发布了太多信息。

这是生成的 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
}

生成“爱好”的函数

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)
}
4

2 回答 2

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

于 2013-06-09T04:15:50.997 回答
0

.Key.IntID像 mjibson 建议的那样使用,或者使用来.Key.Encode获取完整的编码密钥,如果您将祖先与密钥一起使用,这是一个更好的选择,因为在这种情况下不能保证整数 ID 是唯一的。

于 2014-07-08T04:29:58.687 回答