使用Flask和Python为表单中的文本框赋予价值(Giving value to text boxes in forms using Flask and Python)

编程入门 行业动态 更新时间:2024-10-20 15:56:53
使用Flask和Python为表单中的文本框赋予价值(Giving value to text boxes in forms using Flask and Python)

我有一个HTML页面,我有5个文本框,我想用MongoDB数据库中的数据填充。 结构如此:

app/ templates/ *.html (All HTML Templates here) forms.py views.py run.py

所有表单都存储在forms.py文件中。 views.py文件具有路由。 当用户点击某个html页面时,文本框应该已经具有应该来自数据库的值。 我无法弄清楚如何填充这些。

我正在使用flask-wtf表格。 以下是forms.py文件的代码 - https://github.com/rahulw/Inventory-Management-for-a-Store/blob/master/app/forms.py 。

I have a HTML page where I have 5 text boxes which I want to be populated with data from my MongoDB database. The structure is so :

app/ templates/ *.html (All HTML Templates here) forms.py views.py run.py

All the forms are stored in forms.py file. The views.py file has the route. When the user clicks on a certain html page, the text boxes should already have value which should come from the Database. I am not able to figure out how to populate those.

I am using flask-wtf for forms. Here is the code for the forms.py file -- https://github.com/rahulw/Inventory-Management-for-a-Store/blob/master/app/forms.py.

最满意答案

你正在使用什么数据库真的不重要。 基本上,您需要做的是更改表单的此属性:

form.post_body.data = page_content['body']

如果page_content是您从数据库中提取的内容,则post_body是您要填充的特定表单字段,表单当然是您的表单。

然后,您可以像往常一样将此表单放入视图中,并填充数据!

Shouldn't really matter what DB you're using. Basically, what you need to do is change this attribute of your form:

form.post_body.data = page_content['body']

Where page_content is the stuff you've pulled from the DB, post_body is your specific form field you want to populate, and form is of course your form.

You'd then throw this form into the view like you normally would, and the data will be populated!

更多推荐

本文发布于:2023-07-31 02:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340497.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   文本框   价值   Python   Flask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!