django MVC模式 数据库的操作mysql
2017-07-21 17:59
555 查看
介绍:本节课我们继续学习djangoWEB框架的开发,这节课主要是学习如何访问数据库,django如何自动为我们创建好表结构等相关内容。
1、首先我们打开settings.py找到DATABASES关键字,这个是配置我们的数据库。
里面的属性不做介绍了,一看就懂了。
2、添加自己的一个startapp polls,并在settings中配置。
也可以直接写'polls'
3、打开我们刚刚创建的应用polls -->models.py内容如下:
4、在pycharm中运行manage.py
成功后出现:
我们执行三条命令如下:
manage.py@pythondjango > migrate
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py migrate D:/pythondjango
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
Process finished with exit code 0
manage.py@pythondjango > makemigrations polls
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py makemigrations polls D:/pythondjango
Migrations for 'polls':
polls\migrations\0001_initial.py
- Create model Test
Following files were affected
D:\pythondjango\polls\migrations\0001_initial.py
Process finished with exit code 0
文件内容是:
manage.py@pythondjango > migrate polls
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py migrate polls D:/pythondjango
Operations to perform:
Apply all migrations: polls
Running migrations:
Applying polls.0001_initial... OK
Process finished with exit code 0
表名组成结构为:应用名_类名
注意:尽管我们没有在models给表设置主键,但是Django会自动添加一个id作为主键。
操作数据库:
在urls.py中添加一个映射:
向数据库中添加一个值,修改views.py
总结:本节课主要分享django与mysql数据库的搭配使用,如何创建数据库表结构,settings文件中配置数据库,添加自己新创建的应用,如何在models.py中创建一个实例,属性的含义等内 容。同时借助pycharm自动生成数据库表,并且创建表结构是不需要指定主键,django会帮助我们创建一个id。在views.py中向数据库添加值的步骤。
1、首先我们打开settings.py找到DATABASES关键字,这个是配置我们的数据库。
里面的属性不做介绍了,一看就懂了。
2、添加自己的一个startapp polls,并在settings中配置。
也可以直接写'polls'
3、打开我们刚刚创建的应用polls -->models.py内容如下:
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models # Create your models here. class Test(models.Model): name = models.CharField(max_length=20) 解释: 以上的类名代表了数据库表名,且继承了models.Model,类里面的字段代表数据表中的字段(name), 数据类型则由CharField(相当于varchar)、DateField(相当于datetime), max_length 参数限定长度。
4、在pycharm中运行manage.py
成功后出现:
我们执行三条命令如下:
# 创建表结构(内置的django表结构)
manage.py@pythondjango > migrate
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py migrate D:/pythondjango
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
Process finished with exit code 0
让 Django 知道我们在我们的模型有一些变更,生成一个文件
manage.py@pythondjango > makemigrations polls
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py makemigrations polls D:/pythondjango
Migrations for 'polls':
polls\migrations\0001_initial.py
- Create model Test
Following files were affected
D:\pythondjango\polls\migrations\0001_initial.py
Process finished with exit code 0
文件内容是:
# -*- coding: utf-8 -*- # Generated by Django 1.11.3 on 2017-07-21 09:15 from __future__ import unicode_literals from django.db import migrations, models class Migration(migrations.Migration): initial = True dependencies = [ ] operations = [ migrations.CreateModel( name='Test', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=20)), ], ), ] 其实里面记录了数据库的一些变化
manage.py@pythondjango > migrate polls
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py migrate polls D:/pythondjango
Operations to perform:
Apply all migrations: polls
Running migrations:
Applying polls.0001_initial... OK
Process finished with exit code 0
表名组成结构为:应用名_类名
注意:尽管我们没有在models给表设置主键,但是Django会自动添加一个id作为主键。
操作数据库:
在urls.py中添加一个映射:
向数据库中添加一个值,修改views.py
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.http import HttpResponse from polls.models import Test from django.shortcuts import render # Create your views here. # 数据库操作 def testdb(request): test1 = Test(name='温鸿雨') test1.save() return HttpResponse("<p>数据添加成功!</p>") [b]添加成功:[/b]
总结:本节课主要分享django与mysql数据库的搭配使用,如何创建数据库表结构,settings文件中配置数据库,添加自己新创建的应用,如何在models.py中创建一个实例,属性的含义等内 容。同时借助pycharm自动生成数据库表,并且创建表结构是不需要指定主键,django会帮助我们创建一个id。在views.py中向数据库添加值的步骤。
相关文章推荐
- python django 增删改查操作 数据库Mysql
- Django+mysql配置与简单操作数据库实例
- django 增删改查操作 数据库Mysql
- mac之配置mysql到环境变量及使用命令操作数据库
- Ubuntu Mysql 数据库和表的操作 [2P]
- 性能测试工具操作数据库(一)-Loadrunner与Mysql
- 数据库(十)mysql 表操作命令(表创建、修改、删除等)
- 操作大文本数据存储在数据库中 mysql中有个lob
- MySQL之终端:管理数据库的基本操作
- Python3和Django如何支持MySQL和MariaDB数据库
- PHP使用数据库永久连接方式(mysql_pconnect)操作MySQL的是与非
- mysql之sql语句-数据库、表内容相关的基础操作
- 02_Spring Boot 数据库操作 [基于Spring Boot、Mysql、Jpa]
- Django 学习笔记(七)数据库基本操作(增查改删)
- python数据库操作之pymysql模块和sqlalchemy模块(项目必备)
- Pycharm+Django+Python+MySQL开发(一)后台管理数据库
- PHP使用数据库永久连接方式操作MySQL的是与非
- Django从零开始搭建一个相册网站--5.Python操作数据库
- 在PHP中使用MySQL Mysqli操作数据库 ,以及类操作方法
- Django:一些数据库操作的指令(持续完善)