亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Beego Session 不會自動將數據傳遞給模板

Beego Session 不會自動將數據傳遞給模板

Go
手掌心 2021-11-29 15:34:59
在獲取類型為map[string]interface{}with 的會話信息后this.GetSession("session_key"),我確實必須像這樣顯式設置上下文和類型斷言會話,以便顯式地將數據傳遞給模板。// Get the sessionprofile := this.GetSession("profile")// Have to add data to the template's contextthis.Data["nickname"] = profile.(map[string]interface{})["nickname"].(string)this.Data["picture"] = profile.(map[string]interface{})["picture"].(string)// Render templatethis.TplNames = "user.html"會話數據(類型map[string]interface{})如下所示:{"nickname": "joe", "picture": "urltotheimg"}但是,根據 Beego 的 session doc,看起來會話是隱式傳遞的,不需要任何類型斷言或上下文傳遞(模板可以立即訪問會話值,即{{.nickname}}和{{.picture}})這是在重定向到之前設置會話的控制器 /user// Inherit beego's base controllertype MyController struct {    beego.Controller}func (this *MyController) Get() {    // code for getting token here    // Getting the User information    client := conf.Client(oauth2.NoContext, token)    resp, err := client.Get("https://" + domain + "/userinfo")    if err != nil {        this.Redirect("/error", 500)        return    }    // Reading the body for user's information    raw, err := ioutil.ReadAll(resp.Body)    defer resp.Body.Close()    if err != nil {        this.Redirect("/error", 500)        return    }    // Unmarshalling the JSON of the Profile    var profile map[string]interface{}    if err := json.Unmarshal(raw, &profile); err != nil {        this.Redirect("/error", 500)        return    }    // Saving the information to the session.    this.SetSession("profile", profile)    // redirect to /user    this.Redirect("/user", 301)}這是“/user”的控制器type UserController struct {    beego.Controller}只有這樣我才能將模板映射到這樣的數據:<img src="{{ .picture }}"><p>Hello, {{ .nickname }}</p>我很確定有必要設置模板數據。我只是不確定為什么上面的文檔沒有這樣做。任何幫助,將不勝感激。
查看完整描述

1 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

我剛剛嘗試運行Beego 快速入門項目并成功運行。


確保你同時安裝了beego和bee。創建新項目后,請bee new projectname確保編輯projectname/conf/app.conf文件并添加sessionon = true:


appname = quickstart

httpport = 8080

runmode = dev

sessionon = true

我創建了一個重定向控制器,如:


type RedirectController struct {

    beego.Controller

}


func (c *RedirectController) Get() {

    profile := make(map[string]interface{})

    profile["nickname"] = "User's Nickname"

    profile["picture"] = "/path/to/img.jpg"


    c.SetSession("profile", profile)

    c.Redirect("/", 301)

}

主控制器:


type MainController struct {

    beego.Controller

}


func (c *MainController) Get() {

    profile := c.GetSession("profile")


    c.Data["nickname"] = profile.(map[string]interface{})["nickname"]

    c.Data["picture"] = profile.(map[string]interface{})["picture"]

    c.TplNames = "index.tpl"

}

我的 index.tpl 文件:


<p>Nickname: {{.nickname}}</p>

<p>Picture: {{.picture}}</p>

和路由器:


func init() {

    beego.Router("/", &controllers.MainController{})

    beego.Router("/redirect", &controllers.RedirectController{})

}

我還建議您使用結構來存儲配置文件值,例如:


// Define struct.

type Profile struct{

    Nickname string

    Picture  string

}


// Save it for template rendering.

this.Data["profile"] = &Profile{Nickname:"astaxie", Picture:"img.jpg"}


// And render it like this:

Nickname: {{.profile.Nickname}}

Picture:  {{.profile.Picture}}

請務必閱讀本文以了解模板渲染是如何完成的。我希望這就是您所要求的,如果不是,請編輯您的問題并添加更多有用的信息,我將編輯此答案。


查看完整回答
反對 回復 2021-11-29
  • 1 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號