如何执行模板

在此代码中,我正在尝试执行函数中存在的 。tmpl.ExecuteTemplate(res, "about.html", d)showAbout()


在文件中,我有两个函数。第一个是,第二个是。 函数检查授权,授权成功后,进入具有if语句的函数。handler.goshowAbout()about()about()showAbout()


如果数据库中的 about 字段为空,则应执行 ,获取数据并将其插入到数据库中。aboutform.html


如果数据已成功插入,则显示消息并返回以在此处显示数据。about.html


只有不执行。尽管给出了成功消息。about.html


关于.html


<section>

   <h1>About</h1>

   <hr>

   <p>{{.Aboutdata}}</p>

</section>

关于形式.html


<form action="/about" method="POST">

    <section>

        <label>Content</label>

        <textarea name="content"></textarea>

        <div>

            <input type="submit" value="Submit">

        </div>

    </section>

</form>

数据库


func Insertdata(key, value string) bool {

    collection := Connect.Database("webApp3").Collection("data")

    filter := bson.M{"email": Account.Email, "password": Account.Password}

    update := bson.M{

        "$set": bson.M{

            key: value,

        },

    }

    _, err := collection.UpdateOne(context.TODO(), filter, update)

    return err == nil

}

处理程序.go


func showAbout(res http.ResponseWriter, req *http.Request) {

    d := struct{ Aboutdata string }{Aboutdata: database.Account.About}

    if d.Aboutdata == "" {

        tmpl.ExecuteTemplate(res, "aboutform.html", nil)

        content := req.FormValue("content")

        inserted := database.Insertdata("about", content)

        if inserted == true {

            fmt.Println("About is successfully inserted")

            tmpl.ExecuteTemplate(res, "about.html", d)   // It is not executing the about.html file

        } else {

            fmt.Println("About is not inserted")

        }

    } else {

        tmpl.ExecuteTemplate(res, "about.html", d)    // Although this same file is executing here.

    }

}


叮当猫咪
浏览 102回答 1
1回答

互换的青春

下面是你的关于处理程序的外观示例。请记住,该示例只是使用单个处理程序处理 GET 和 POST 请求的通用结构的图示。据我所知,您问题中的代码包含许多其他逻辑错误,您仍然需要解决这些错误。请注意,呈现模板和处理数据输入的逻辑在 GET 和 POST HTTP 方法之间拆分。某些路由器允许基于方法的处理程序注册,在这种情况下,您可以有两个单独的处理程序,一个用于,另一个用于或某物。showAboutcreateAbout考虑到块的结构,此示例中的语句的使用是不必要的,但是,我确实包含它们以明确说明,一般来说,一旦您写入响应,您就不应该有任何其他响应写入代码:no ,不再调用等。returnif-elsehttp.RedirectExecuteTemplatefunc handleAbout(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; if r.Method == "GET" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if data_is_present {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if err := t.ExecuteTemplate(w, "about.html", nil); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if data_is_NOT_present {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if err := t.ExecuteTemplate(w, "aboutform.html", nil); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; } else if r.Method == "POST" {&nbsp; &nbsp; &nbsp; &nbsp; content := r.FormValue("content")&nbsp; &nbsp; &nbsp; &nbsp; inserted := database.Insertdata("about", content)&nbsp; &nbsp; &nbsp; &nbsp; if inserted == true {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d := struct{ Aboutdata string }{Aboutdata: content}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err := t.ExecuteTemplate(w, "about.html", d); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("About is not inserted")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go