Django - 向 MySQL 数据库添加行

Django - Add rows to MySQL database(Django - 向 MySQL 数据库添加行)

本文介绍了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 数据库添加行

基础教程推荐