在此代码中,我正在尝试执行函数中存在的 。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.
}
}
叮当猫咪
互换的青春
随时随地看视频慕课网APP
相关分类