目录

python学习之-Django-连接数据库mysql创建model

目录

python学习之–Django–连接数据库mysql创建model

1.

建立一个project

https://img-blog.csdn.net/20150417104628672

2.

创建第一个页面

https://img-blog.csdn.net/20150417105143043

3.

创建一个app

https://img-blog.csdn.net/20150417105024132

4.

然后给app增加一个页面

4.1

在setting里面配置:

https://img-blog.csdn.net/20150417105255686

在my_project的url中添加

https://img-blog.csdn.net/20150417124024260

在app1的urls中添加

https://img-blog.csdn.net/20150417124508970

创建views

https://img-blog.csdn.net/20150417110400036

然后

https://img-blog.csdn.net/20150417110429270

就可以通过127.0.0.1:8000  访问了

5.

配置数据库

在setting中修改database信息

https://img-blog.csdn.net/20150417110532210

check看是否连接上数据库

https://img-blog.csdn.net/20150417110834652

在我的workbench中可以看到生成了默认的table

https://img-blog.csdn.net/20150417110758928

接下来我们自己建立model (需要继承models.Model)

https://img-blog.csdn.net/20150417111025740

运行命令

https://img-blog.csdn.net/20150417111251014

然后再运行

https://img-blog.csdn.net/20150417111404412

可以发现table创建完毕

https://img-blog.csdn.net/20150417111511913

接下来对数据库进行操作

我先在mysql中插入2条数据

https://img-blog.csdn.net/20150417111829231

查看发现数据已经成功插入

https://img-blog.csdn.net/20150417112044053

然后我们

修改model让其返回name,修改views让其在页面上显示返回的结果

https://img-blog.csdn.net/20150417124509117

https://img-blog.csdn.net/20150417124359238

运行之后可以看到结果

https://img-blog.csdn.net/20150417124734041

整个流程大致就是这样

查看table结构的mysql命令

show databases;

use 数据库名;

show tables;