Django - Add rows to MySQL database(Django - 向 MySQL 数据库添加行)
问题描述
所以我已经有一个数据库设置,其中已经插入了几列和几行.我正在尝试创建一个视图,您只需将信息输入表单并按提交,然后将添加一行包含您刚刚输入的信息的 MySQL 数据库.
So I already have a database setup with a few columns and a few rows already inserted in. I'm trying to create a view that you would just input information into a form and press Submit, then a row would be added to the MySQL database with the information you just typed in.
我相信您可以使用 admin 执行此操作,但我想在没有 admin 的情况下尝试,我不确定这是否可行?到目前为止,我一直在使用 MySQL 命令行添加行..
I believe you can do this with admin, but I would like to try without admin and I'm not sure if this is possible? I've been using the MySQL commandline to add rows as of now..
推荐答案
当然这是可能的,这是数据驱动网站的构建块.您可以使用 Daniel 建议的 ModelForm(他们免费提供内置验证和 HTML 标记)来轻松地将您的模型映射到前端表单.首先从 django 教程或文档开始可能会有所帮助.
Of coures this is possible this is a building block for data driven websites. You can use a ModelForm as Daniel suggested (they offer built in validation and HTML markup for FREE) to easily map your model to a front end form. It would probably be beneficial to start with django tutorial or documentation first.
最基本的,你所要做的就是实例化你的模型
At the the very basic, all you have to do is instantiate your model
new_entry = YourModel(name='me', age='222', about='stackoverflow')
然后保存
new_entry.save()
这会将其作为新行添加到您的数据库中.
This adds it as a new row to your db.
https://docs.djangoproject.com/en/dev/主题/数据库/模型/
这篇关于Django - 向 MySQL 数据库添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django - 向 MySQL 数据库添加行


- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01