django 2.0(一) 安装与连接MySQL

Python
安装 <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/django" title="View all posts in django" target="_blank">django</a></span>2.0不支持python2.x,所以安装<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/python3" title="View all posts in python3" target="_blank">python3</a></span>.x。 # linux安装<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/python3" title="View all posts in python3" target="_blank">python3</a></span> $ sudo apt-get install <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/python3" title="View all posts in python3" target="_blank">python3</a></span> mac安装python3 $ brew install python3 安装<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/django" title="View all posts in django" target="_blank">django</a></span> 2.0: $ pip3 install django==2.0 站点初始化 1. 初始化 $ django-admin startproject mysite 主要文件结构: mysite/ ├── db.sqlite3 # 自带数据库文件 ├── manage.py # 项目管理文件 └── mysite ├── __init__.py ├── settings.py # 全局设置文件 ├── urls.py # 全局路由控制 └── wsgi.py # 服务器部署文件 2. 站点预览 $ cd mysite $ python3 manage.py runserver 注:runserver后面可指定ip及端口号,如下 $ python3 manage.py runserver 127.0.0.1:8000 连接MySQL数据库 1. 安装django与mysql的连接工具mysqlclient $ sudo pip3 install mysqlclient 注:若使用3.6以下版本的<span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span>,则须使用pymysql代替mysqlclient $ sudo pip3 install pymysql 并在mysite/mysite/__init__.py文件加入以下代码: import pymysql pymysql.install_as_MySQLdb() 2. 配置数据库信息 修改mysite/mysite/settings.py文件代码: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test', 'USER': 'root', 'PASSWORD': '123456', 'HOST':'localhost', 'PORT':'3306', } } 3. 新建app $ django-admin startapp TestModel 4. 修改mysite/TestModel/models.py文件代码(定义模型): from django.db import models class Test(models.Model): name = models.CharField(max_length=20) 5. 修改mysite/mysite/settings.py文件代码: INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'TestModel', # 添加此项 ) 6. 迁移表结构到数据 $ python3 manage.py makemigrations # 制造迁移 $ python3 manage.py migrate # 迁移 注:若提示django.db.utils.InternalError: (1049, "Unknown database 'test'"),则需先手动在mysql创建test数据库 数据库操作 1. 添加数据 添加mysite/mysite/testdb.py文件,内容如下: # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Test # 数据库操作 def testdb(request): test1 = Test(name='luna') test1.save() return HttpResponse("<p>数据添加成功!</p>") 修改mysite/mysite/urls.py文件代码: from django.contrib import admin from django.urls import path from django.conf.urls import * from . import testdb urlpatterns = [ path('admin/', admin.site.urls), url(r'^testdb$', testdb.testdb), ] 访问 http://127.0.0.1:8000/testdb 即可将数据添加到数据库。 注:若出现utf-8编码问题,用记事本将文件转utf-8编码格式即可。 2. 删除数据 修改mysite/mysite/testdb.py文件代码: # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Test # 数据库操作 def testdb(request): # 删除id=1的数据 test1 = Test.objects.get(id=1) test1.delete() # 另外一种方式 # Test.objects.filter(id=1).delete() # 删除所有数据 # Test.objects.all().delete() return HttpResponse("<p>删除成功</p>") 访问 http://127.0.0.1:8000/testdb 即可将数据从数据库删除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
安装
django2 . 0不支持 python2 . x,所以安装 python3 . x。
 
# linux安装python3
$ sudo apt - get install python3
mac安装 python3
$ brew install python3
安装 django 2.0:
 
$ pip3 install django == 2.0
站点初始化
1. 初始化
 
$ django - admin startproject mysite
主要文件结构:
mysite /
├── db . sqlite3    # 自带数据库文件
├── manage . py    # 项目管理文件
└── mysite
    ├── __init__ . py     
    ├── settings . py    # 全局设置文件
    ├── urls . py        # 全局路由控制
    └── wsgi . py        # 服务器部署文件
2. 站点预览
 
$ cd mysite
$ python3 manage . py runserver
注: runserver后面可指定 ip及端口号,如下
 
$ python3 manage . py runserver 127.0.0.1 : 8000
连接 MySQL数据库
1. 安装 django与 mysql的连接工具 mysqlclient
 
$ sudo pip3 install mysqlclient
注:若使用 3.6以下版本的 python,则须使用 pymysql代替 mysqlclient
 
$ sudo pip3 install pymysql
并在 mysite / mysite / __init__ . py文件加入以下代码:
 
import pymysql
pymysql . install_as_MySQLdb ( )
2. 配置数据库信息
修改 mysite / mysite / settings . py文件代码:
 
DATABASES = {
     'default' : {
         'ENGINE' : 'django.db.backends.mysql' ,
         'NAME' : 'test' ,
         'USER' : 'root' ,
         'PASSWORD' : '123456' ,
         'HOST' : 'localhost' ,
         'PORT' : '3306' ,
     }
}
3. 新建 app
 
$ django - admin startapp TestModel
4. 修改 mysite / TestModel / models . py文件代码(定义模型):
 
from django . db import models
class Test ( models . Model ) :
     name = models . CharField ( max_length = 20 )
5. 修改 mysite / mysite / settings . py文件代码:
 
INSTALLED_APPS = (
     'django.contrib.admin' ,
     'django.contrib.auth' ,
     'django.contrib.contenttypes' ,
     'django.contrib.sessions' ,
     'django.contrib.messages' ,
     'django.contrib.staticfiles' ,
     'TestModel' ,    # 添加此项
)
6. 迁移表结构到数据
 
$ python3 manage . py makemigrations    # 制造迁移
$ python3 manage . py migrate    # 迁移
注:若提示 django . db . utils . InternalError : ( 1049 , "Unknown database 'test'" ),则需先手动在 mysql创建 test数据库
 
数据库操作
1. 添加数据
添加 mysite / mysite / testdb . py文件,内容如下:
 
# -*- coding: utf-8 -*-
from django . http import HttpResponse
from TestModel . models import Test
# 数据库操作
def testdb ( request ) :
     test1 = Test ( name = 'luna' )
     test1 . save ( )
     return HttpResponse ( "<p>数据添加成功!</p>" )
修改 mysite / mysite / urls . py文件代码:
 
from django . contrib import admin
from django . urls import path
 
from django . conf . urls import *
from . import testdb
 
urlpatterns = [
     path ( 'admin/' , admin . site . urls ) ,
     url ( r '^testdb$' , testdb . testdb ) ,
]
访问 http : / / 127.0.0.1 : 8000 / testdb 即可将数据添加到数据库。
注:若出现 utf - 8编码问题,用记事本将文件转 utf - 8编码格式即可。
2. 删除数据
修改 mysite / mysite / testdb . py文件代码:
 
# -*- coding: utf-8 -*-
from django . http import HttpResponse
from TestModel . models import Test
# 数据库操作
def testdb ( request ) :
     # 删除id=1的数据
     test1 = Test . objects . get ( id = 1 )
     test1 . delete ( )
    
     # 另外一种方式
     # Test.objects.filter(id=1).delete()
    
     # 删除所有数据
     # Test.objects.all().delete()
    
     return HttpResponse ( "<p>删除成功</p>" )
访问 http : / / 127.0.0.1 : 8000 / testdb 即可将数据从数据库删除。




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值