单个Golang Web模板中数据库中的多个表单

发布于 2025-01-21 10:16:28 字数 995 浏览 1 评论 0原文

我正在Golang做我的第一个项目,并且正在尝试创建一个带有两个列表的表单,请选择以下

内容:带有表单的网页,它有两个列表可从中选择选项和一个文本字段

但是,如果我想简单地从SQLITE数据库中从两个表中从两个表中检索其条目,则“模板”仅允许我每个模板发送一个查询,因此

func Person(w http.ResponseWriter, r *http.Request) {
    db := ConnectDB()
    arrP := GetPerson(db)
    templates.ExecuteTemplate(w, "person", arrP)
}

,只有第一个列表才能从范围内提取信息(在这种特殊情况下,它将从“人”表中显示有效的选项,第二个列表将显示有效的选项 编码的条目


db := ConnectDB()

列表将继续显示我自己 在 templates.executetemplate()中,

templates.ExecuteTemplate(w, "person", arrP)

它将渲染“人” html模板。如果我尝试类似的事情

func Person(w http.ResponseWriter, r *http.Request) {
    db := ConnectDB()
    arrP := GetPerson(db)
    arrS := GetService(db)
    templates.ExecuteTemplate(w, "person", arrP, arrS)
}

,将会抱怨,因为我的论点比预期的要多。因此,只有人员列表才能通过数据库中的信息正确渲染;但是,服务列表没有任何来源可以从中提取其条目。

I'm doing my first project in Golang, and I'm trying to create a form with two lists to choose options from, as following:

webpage with a form, it has two lists to choose options from and a text field

However, If I want to make those lists to retrieve their entries from two tables from a SQLite database, the module "templates" only allows me to send one query per template, something like

func Person(w http.ResponseWriter, r *http.Request) {
    db := ConnectDB()
    arrP := GetPerson(db)
    templates.ExecuteTemplate(w, "person", arrP)
}

Thus, only the first list will pull up information from the range (in this particular case, it'll display valid options from the 'person' table, and the second list will keep showing the entries I hard coded myself.


Thanks @Zombo for replying. To clarify some things, the function I posted opens up the connection with the database

db := ConnectDB()

Then it creates arrP (array of 'person' type) and calls the built-in templates.ExecuteTemplate()

templates.ExecuteTemplate(w, "person", arrP)

Which will render the "person" html template. However, in that same page I'm trying to put another selection form to choose 'Service'. If I try something like

func Person(w http.ResponseWriter, r *http.Request) {
    db := ConnectDB()
    arrP := GetPerson(db)
    arrS := GetService(db)
    templates.ExecuteTemplate(w, "person", arrP, arrS)
}

It will complain because I'm passing more arguments than expected. Therefore, only the list of persons will render properly with the information from the database; however, the list of services doesn't have any source to pull its entries from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2025-01-28 10:16:28

@zombo给出了一个整洁的答案,使用 map < / em>,我们可以将多个数组传递到模板以渲染多个列表 /表。

map[string]any{“arrP”: arrP, “arrS”: arrS}

然后在.tmpl文件中使用 $。arrs

@Zombo gave a neat answer, using a map we can pass multiple arrays to a template to render several list / tables.

map[string]any{“arrP”: arrP, “arrS”: arrS}

And then use $.arrS in the .tmpl file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文