第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Beego Session 不會(huì)自動(dòng)將數(shù)據(jù)傳遞給模板

Beego Session 不會(huì)自動(dòng)將數(shù)據(jù)傳遞給模板

Go
手掌心 2021-11-29 15:34:59
在獲取類型為map[string]interface{}with 的會(huì)話信息后this.GetSession("session_key"),我確實(shí)必須像這樣顯式設(shè)置上下文和類型斷言會(huì)話,以便顯式地將數(shù)據(jù)傳遞給模板。// 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"會(huì)話數(shù)據(jù)(類型map[string]interface{})如下所示:{"nickname": "joe", "picture": "urltotheimg"}但是,根據(jù) Beego 的 session doc,看起來會(huì)話是隱式傳遞的,不需要任何類型斷言或上下文傳遞(模板可以立即訪問會(huì)話值,即{{.nickname}}和{{.picture}})這是在重定向到之前設(shè)置會(huì)話的控制器 /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}只有這樣我才能將模板映射到這樣的數(shù)據(jù):<img src="{{ .picture }}"><p>Hello, {{ .nickname }}</p>我很確定有必要設(shè)置模板數(shù)據(jù)。我只是不確定為什么上面的文檔沒有這樣做。任何幫助,將不勝感激。
查看完整描述

1 回答

?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

我剛剛嘗試運(yùn)行Beego 快速入門項(xiàng)目并成功運(yùn)行。


確保你同時(shí)安裝了beego和bee。創(chuàng)建新項(xiàng)目后,請(qǐng)bee new projectname確保編輯projectname/conf/app.conf文件并添加sessionon = true:


appname = quickstart

httpport = 8080

runmode = dev

sessionon = true

我創(chuàng)建了一個(gè)重定向控制器,如:


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

}

我還建議您使用結(jié)構(gòu)來存儲(chǔ)配置文件值,例如:


// 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}}

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


查看完整回答
反對(duì) 回復(fù) 2021-11-29
  • 1 回答
  • 0 關(guān)注
  • 158 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)