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 数据库添加行
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 无法解决整理冲突 2021-01-01
