Django – models:MySQL
学习笔记。
PIP:mysqlclient
1 2 3 4 5 6 7 8 9 10 |
[root@center-me ~]# pip install mysqlclient Collecting mysqlclient Using cached mysqlclient-1.3.12.tar.gz Building wheels for collected packages: mysqlclient Running setup.py bdist_wheel for mysqlclient ... done Stored in directory: /root/.cache/pip/wheels/df/bb/60/bf7c315cbe163515db1c846e4ffa5557dd785c82e82f3492e8 Successfully built mysqlclient Installing collected packages: mysqlclient Successfully installed mysqlclient-1.3.12 [root@center-me ~]# |
这一步不总是一帆风顺的,可能会遇到这样的问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@center-me ~]# pip install mysqlclient Collecting mysqlclient Using cached mysqlclient-1.3.12.tar.gz Complete output from command python setup.py egg_info: sh: mysql_config: command not found Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-build-cXhDLG/mysqlclient/setup.py", line 17, in <module> metadata, options = get_config() File "setup_posix.py", line 44, in get_config libs = mysql_config("libs_r") File "setup_posix.py", line 26, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-cXhDLG/mysqlclient/ [root@center-me ~]# [root@center-me ~]# |
解决,安装mysql-devel
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
[root@center-me ~]# yum install mysql-devel Loaded plugins: fastestmirror, langpacks base | 3.6 kB 00:00:00 epel | 4.7 kB 00:00:00 extras | 3.4 kB 00:00:00 home_libertas-ict_cobbler28 | 1.3 kB 00:00:00 salt-latest | 2.9 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): epel/x86_64/updateinfo | 851 kB 00:00:01 (2/2): epel/x86_64/primary_db | 6.1 MB 00:00:02 Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.sohu.com * updates: mirrors.shuosc.org Resolving Dependencies --> Running transaction check ---> Package mariadb-devel.x86_64 1:5.5.56-2.el7 will be installed --> Processing Dependency: openssl-devel(x86-64) for package: 1:mariadb-devel-5.5.56-2.el7.x86_64 --> Running transaction check ---> Package openssl-devel.x86_64 1:1.0.2k-8.el7 will be installed --> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-8.el7 for package: 1:openssl-devel-1.0.2k-8.el7.x86_64 --> Processing Dependency: zlib-devel(x86-64) for package: 1:openssl-devel-1.0.2k-8.el7.x86_64 --> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-8.el7.x86_64 --> Running transaction check ---> Package krb5-devel.x86_64 0:1.15.1-8.el7 will be installed --> Processing Dependency: libkadm5(x86-64) = 1.15.1-8.el7 for package: krb5-devel-1.15.1-8.el7.x86_64 --> Processing Dependency: krb5-libs(x86-64) = 1.15.1-8.el7 for package: krb5-devel-1.15.1-8.el7.x86_64 --> Processing Dependency: libverto-devel for package: krb5-devel-1.15.1-8.el7.x86_64 --> Processing Dependency: libselinux-devel for package: krb5-devel-1.15.1-8.el7.x86_64 --> Processing Dependency: libcom_err-devel for package: krb5-devel-1.15.1-8.el7.x86_64 --> Processing Dependency: keyutils-libs-devel for package: krb5-devel-1.15.1-8.el7.x86_64 --> Processing Dependency: libkdb5.so.8()(64bit) for package: krb5-devel-1.15.1-8.el7.x86_64 ---> Package openssl-libs.x86_64 1:1.0.1e-42.el7 will be updated --> Processing Dependency: openssl-libs(x86-64) = 1:1.0.1e-42.el7 for package: 1:openssl-1.0.1e-42.el7.x86_64 ---> Package openssl-libs.x86_64 1:1.0.2k-8.el7 will be an update ---> Package zlib-devel.x86_64 0:1.2.7-17.el7 will be installed --> Processing Dependency: zlib = 1.2.7-17.el7 for package: zlib-devel-1.2.7-17.el7.x86_64 --> Running transaction check ---> Package keyutils-libs-devel.x86_64 0:1.5.8-3.el7 will be installed ---> Package krb5-libs.x86_64 0:1.12.2-14.el7 will be updated --> Processing Dependency: krb5-libs(x86-64) = 1.12.2-14.el7 for package: krb5-workstation-1.12.2-14.el7.x86_64 --> Processing Dependency: libkadm5srv_mit.so.9()(64bit) for package: krb5-workstation-1.12.2-14.el7.x86_64 --> Processing Dependency: libkadm5srv_mit.so.9(kadm5srv_mit_9_MIT)(64bit) for package: krb5-workstation-1.12.2-14.el7.x86_64 --> Processing Dependency: libkdb5.so.7()(64bit) for package: krb5-workstation-1.12.2-14.el7.x86_64 --> Processing Dependency: libkdb5.so.7(kdb5_7_MIT)(64bit) for package: krb5-workstation-1.12.2-14.el7.x86_64 ---> Package krb5-libs.x86_64 0:1.15.1-8.el7 will be an update ---> Package libcom_err-devel.x86_64 0:1.42.9-10.el7 will be installed --> Processing Dependency: libcom_err(x86-64) = 1.42.9-10.el7 for package: libcom_err-devel-1.42.9-10.el7.x86_64 ---> Package libkadm5.x86_64 0:1.15.1-8.el7 will be installed ---> Package libselinux-devel.x86_64 0:2.5-11.el7 will be installed --> Processing Dependency: libselinux(x86-64) = 2.5-11.el7 for package: libselinux-devel-2.5-11.el7.x86_64 --> Processing Dependency: libsepol-devel(x86-64) >= 2.5-6 for package: libselinux-devel-2.5-11.el7.x86_64 --> Processing Dependency: pkgconfig(libsepol) for package: libselinux-devel-2.5-11.el7.x86_64 --> Processing Dependency: pkgconfig(libpcre) for package: libselinux-devel-2.5-11.el7.x86_64 ---> Package libverto-devel.x86_64 0:0.2.5-4.el7 will be installed ---> Package openssl.x86_64 1:1.0.1e-42.el7 will be updated ---> Package openssl.x86_64 1:1.0.2k-8.el7 will be an update ---> Package zlib.x86_64 0:1.2.7-13.el7 will be updated ---> Package zlib.x86_64 0:1.2.7-17.el7 will be an update --> Running transaction check ---> Package krb5-workstation.x86_64 0:1.12.2-14.el7 will be updated ---> Package krb5-workstation.x86_64 0:1.15.1-8.el7 will be an update ---> Package libcom_err.x86_64 0:1.42.9-7.el7 will be updated --> Processing Dependency: libcom_err(x86-64) = 1.42.9-7.el7 for package: e2fsprogs-libs-1.42.9-7.el7.x86_64 --> Processing Dependency: libcom_err(x86-64) = 1.42.9-7.el7 for package: e2fsprogs-1.42.9-7.el7.x86_64 --> Processing Dependency: libcom_err(x86-64) = 1.42.9-7.el7 for package: libss-1.42.9-7.el7.x86_64 ---> Package libcom_err.x86_64 0:1.42.9-10.el7 will be an update ---> Package libselinux.x86_64 0:2.5-6.el7 will be updated --> Processing Dependency: libselinux(x86-64) = 2.5-6.el7 for package: libselinux-python-2.5-6.el7.x86_64 --> Processing Dependency: libselinux(x86-64) = 2.5-6.el7 for package: libselinux-utils-2.5-6.el7.x86_64 ---> Package libselinux.x86_64 0:2.5-11.el7 will be an update ---> Package libsepol-devel.x86_64 0:2.5-6.el7 will be installed ---> Package pcre-devel.x86_64 0:8.32-17.el7 will be installed --> Processing Dependency: pcre(x86-64) = 8.32-17.el7 for package: pcre-devel-8.32-17.el7.x86_64 --> Running transaction check ---> Package e2fsprogs.x86_64 0:1.42.9-7.el7 will be updated ---> Package e2fsprogs.x86_64 0:1.42.9-10.el7 will be an update ---> Package e2fsprogs-libs.x86_64 0:1.42.9-7.el7 will be updated ---> Package e2fsprogs-libs.x86_64 0:1.42.9-10.el7 will be an update ---> Package libselinux-python.x86_64 0:2.5-6.el7 will be updated ---> Package libselinux-python.x86_64 0:2.5-11.el7 will be an update ---> Package libselinux-utils.x86_64 0:2.5-6.el7 will be updated ---> Package libselinux-utils.x86_64 0:2.5-11.el7 will be an update ---> Package libss.x86_64 0:1.42.9-7.el7 will be updated ---> Package libss.x86_64 0:1.42.9-10.el7 will be an update ---> Package pcre.x86_64 0:8.32-14.el7 will be updated ---> Package pcre.x86_64 0:8.32-17.el7 will be an update --> Finished Dependency Resolution Dependencies Resolved =========================================================================================================================================================================================== Package Arch Version Repository Size =========================================================================================================================================================================================== Installing: mariadb-devel x86_64 1:5.5.56-2.el7 base 752 k Installing for dependencies: keyutils-libs-devel x86_64 1.5.8-3.el7 base 37 k krb5-devel x86_64 1.15.1-8.el7 base 266 k libcom_err-devel x86_64 1.42.9-10.el7 base 31 k libkadm5 x86_64 1.15.1-8.el7 base 174 k libselinux-devel x86_64 2.5-11.el7 base 186 k libsepol-devel x86_64 2.5-6.el7 base 74 k libverto-devel x86_64 0.2.5-4.el7 base 12 k openssl-devel x86_64 1:1.0.2k-8.el7 base 1.5 M pcre-devel x86_64 8.32-17.el7 base 480 k zlib-devel x86_64 1.2.7-17.el7 base 50 k Updating for dependencies: e2fsprogs x86_64 1.42.9-10.el7 base 698 k e2fsprogs-libs x86_64 1.42.9-10.el7 base 166 k krb5-libs x86_64 1.15.1-8.el7 base 747 k krb5-workstation x86_64 1.15.1-8.el7 base 811 k libcom_err x86_64 1.42.9-10.el7 base 40 k libselinux x86_64 2.5-11.el7 base 162 k libselinux-python x86_64 2.5-11.el7 base 234 k libselinux-utils x86_64 2.5-11.el7 base 151 k libss x86_64 1.42.9-10.el7 base 45 k openssl x86_64 1:1.0.2k-8.el7 base 492 k openssl-libs x86_64 1:1.0.2k-8.el7 base 1.2 M pcre x86_64 8.32-17.el7 base 422 k zlib x86_64 1.2.7-17.el7 base 90 k Transaction Summary =========================================================================================================================================================================================== Install 1 Package (+10 Dependent packages) Upgrade ( 13 Dependent packages) Total size: 8.7 M Total download size: 2.9 M Is this ok [y/d/N]: y Downloading packages: (1/9): keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm | 37 kB 00:00:01 (2/9): libsepol-devel-2.5-6.el7.x86_64.rpm | 74 kB 00:00:00 (3/9): libverto-devel-0.2.5-4.el7.x86_64.rpm | 12 kB 00:00:00 (4/9): krb5-devel-1.15.1-8.el7.x86_64.rpm | 266 kB 00:00:01 (5/9): libcom_err-devel-1.42.9-10.el7.x86_64.rpm | 31 kB 00:00:01 (6/9): libselinux-devel-2.5-11.el7.x86_64.rpm | 186 kB 00:00:00 (7/9): mariadb-devel-5.5.56-2.el7.x86_64.rpm | 752 kB 00:00:00 (8/9): zlib-devel-1.2.7-17.el7.x86_64.rpm | 50 kB 00:00:00 (9/9): openssl-devel-1.0.2k-8.el7.x86_64.rpm | 1.5 MB 00:00:00 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 1.1 MB/s | 2.9 MB 00:00:02 Running transaction check Running transaction test Transaction test succeeded Running transaction Updating : libcom_err-1.42.9-10.el7.x86_64 1/37 Updating : zlib-1.2.7-17.el7.x86_64 2/37 Updating : libss-1.42.9-10.el7.x86_64 3/37 Updating : pcre-8.32-17.el7.x86_64 4/37 Updating : libselinux-2.5-11.el7.x86_64 5/37 Updating : 1:openssl-libs-1.0.2k-8.el7.x86_64 6/37 Updating : krb5-libs-1.15.1-8.el7.x86_64 7/37 Installing : libkadm5-1.15.1-8.el7.x86_64 8/37 Installing : pcre-devel-8.32-17.el7.x86_64 9/37 Installing : zlib-devel-1.2.7-17.el7.x86_64 10/37 Installing : libcom_err-devel-1.42.9-10.el7.x86_64 11/37 Updating : e2fsprogs-libs-1.42.9-10.el7.x86_64 12/37 Installing : libverto-devel-0.2.5-4.el7.x86_64 13/37 Installing : libsepol-devel-2.5-6.el7.x86_64 14/37 Installing : libselinux-devel-2.5-11.el7.x86_64 15/37 Installing : keyutils-libs-devel-1.5.8-3.el7.x86_64 16/37 Installing : krb5-devel-1.15.1-8.el7.x86_64 17/37 Installing : 1:openssl-devel-1.0.2k-8.el7.x86_64 18/37 Installing : 1:mariadb-devel-5.5.56-2.el7.x86_64 19/37 Updating : e2fsprogs-1.42.9-10.el7.x86_64 20/37 Updating : krb5-workstation-1.15.1-8.el7.x86_64 21/37 Updating : 1:openssl-1.0.2k-8.el7.x86_64 22/37 Updating : libselinux-python-2.5-11.el7.x86_64 23/37 Updating : libselinux-utils-2.5-11.el7.x86_64 24/37 Cleanup : 1:openssl-1.0.1e-42.el7.x86_64 25/37 Cleanup : krb5-workstation-1.12.2-14.el7.x86_64 26/37 Cleanup : krb5-libs-1.12.2-14.el7.x86_64 27/37 Cleanup : 1:openssl-libs-1.0.1e-42.el7.x86_64 28/37 Cleanup : e2fsprogs-1.42.9-7.el7.x86_64 29/37 Cleanup : e2fsprogs-libs-1.42.9-7.el7.x86_64 30/37 Cleanup : libss-1.42.9-7.el7.x86_64 31/37 Cleanup : libselinux-utils-2.5-6.el7.x86_64 32/37 Cleanup : libselinux-python-2.5-6.el7.x86_64 33/37 Cleanup : libselinux-2.5-6.el7.x86_64 34/37 Cleanup : pcre-8.32-14.el7.x86_64 35/37 Cleanup : libcom_err-1.42.9-7.el7.x86_64 36/37 Cleanup : zlib-1.2.7-13.el7.x86_64 37/37 Verifying : libss-1.42.9-10.el7.x86_64 1/37 Verifying : zlib-devel-1.2.7-17.el7.x86_64 2/37 Verifying : keyutils-libs-devel-1.5.8-3.el7.x86_64 3/37 Verifying : libselinux-2.5-11.el7.x86_64 4/37 Verifying : libselinux-python-2.5-11.el7.x86_64 5/37 Verifying : 1:mariadb-devel-5.5.56-2.el7.x86_64 6/37 Verifying : libselinux-utils-2.5-11.el7.x86_64 7/37 Verifying : libsepol-devel-2.5-6.el7.x86_64 8/37 Verifying : pcre-8.32-17.el7.x86_64 9/37 Verifying : libkadm5-1.15.1-8.el7.x86_64 10/37 Verifying : 1:openssl-1.0.2k-8.el7.x86_64 11/37 Verifying : libverto-devel-0.2.5-4.el7.x86_64 12/37 Verifying : libcom_err-devel-1.42.9-10.el7.x86_64 13/37 Verifying : 1:openssl-libs-1.0.2k-8.el7.x86_64 14/37 Verifying : zlib-1.2.7-17.el7.x86_64 15/37 Verifying : 1:openssl-devel-1.0.2k-8.el7.x86_64 16/37 Verifying : libselinux-devel-2.5-11.el7.x86_64 17/37 Verifying : krb5-workstation-1.15.1-8.el7.x86_64 18/37 Verifying : libcom_err-1.42.9-10.el7.x86_64 19/37 Verifying : e2fsprogs-libs-1.42.9-10.el7.x86_64 20/37 Verifying : pcre-devel-8.32-17.el7.x86_64 21/37 Verifying : krb5-devel-1.15.1-8.el7.x86_64 22/37 Verifying : e2fsprogs-1.42.9-10.el7.x86_64 23/37 Verifying : krb5-libs-1.15.1-8.el7.x86_64 24/37 Verifying : libcom_err-1.42.9-7.el7.x86_64 25/37 Verifying : libselinux-python-2.5-6.el7.x86_64 26/37 Verifying : libselinux-utils-2.5-6.el7.x86_64 27/37 Verifying : pcre-8.32-14.el7.x86_64 28/37 Verifying : e2fsprogs-libs-1.42.9-7.el7.x86_64 29/37 Verifying : 1:openssl-1.0.1e-42.el7.x86_64 30/37 Verifying : krb5-workstation-1.12.2-14.el7.x86_64 31/37 Verifying : zlib-1.2.7-13.el7.x86_64 32/37 Verifying : libselinux-2.5-6.el7.x86_64 33/37 Verifying : 1:openssl-libs-1.0.1e-42.el7.x86_64 34/37 Verifying : e2fsprogs-1.42.9-7.el7.x86_64 35/37 Verifying : libss-1.42.9-7.el7.x86_64 36/37 Verifying : krb5-libs-1.12.2-14.el7.x86_64 37/37 Installed: mariadb-devel.x86_64 1:5.5.56-2.el7 Dependency Installed: keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-8.el7 libcom_err-devel.x86_64 0:1.42.9-10.el7 libkadm5.x86_64 0:1.15.1-8.el7 libselinux-devel.x86_64 0:2.5-11.el7 libsepol-devel.x86_64 0:2.5-6.el7 libverto-devel.x86_64 0:0.2.5-4.el7 openssl-devel.x86_64 1:1.0.2k-8.el7 pcre-devel.x86_64 0:8.32-17.el7 zlib-devel.x86_64 0:1.2.7-17.el7 Dependency Updated: e2fsprogs.x86_64 0:1.42.9-10.el7 e2fsprogs-libs.x86_64 0:1.42.9-10.el7 krb5-libs.x86_64 0:1.15.1-8.el7 krb5-workstation.x86_64 0:1.15.1-8.el7 libcom_err.x86_64 0:1.42.9-10.el7 libselinux.x86_64 0:2.5-11.el7 libselinux-python.x86_64 0:2.5-11.el7 libselinux-utils.x86_64 0:2.5-11.el7 libss.x86_64 0:1.42.9-10.el7 openssl.x86_64 1:1.0.2k-8.el7 openssl-libs.x86_64 1:1.0.2k-8.el7 pcre.x86_64 0:8.32-17.el7 zlib.x86_64 0:1.2.7-17.el7 Complete! You have new mail in /var/spool/mail/root [root@center-me ~]# |
安装完成后,就可以找到mysql_config了:
1 2 3 |
[root@center-me ~]# whereis mysql_config mysql_config: /usr/bin/mysql_config /usr/share/man/man1/mysql_config.1.gz [root@center-me ~]# |
创建:Project & APP:
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 |
[root@center-me ~]# cd /script/python/django/ [root@center-me django]# ls -ltr total 0 drwxr-xr-x 3 root root 39 Dec 4 10:27 HelloWorld [root@center-me django]# [root@center-me django]# django-admin startproject modles_me [root@center-me django]# cd modles_me/ [root@center-me modles_me]# [root@center-me modles_me]# django-admin startapp mysql_app [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr total 4 -rw-r--r-- 1 root root 252 Dec 4 22:03 manage.py drwxr-xr-x 2 root root 70 Dec 4 22:03 modles_me drwxr-xr-x 2 root root 85 Dec 4 22:03 mysql_app [root@center-me modles_me]# [root@center-me modles_me]# tree /script/python/django/modles_me/ /script/python/django/modles_me/ ├── manage.py ├── modles_me │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── mysql_app ├── admin.py ├── __init__.py ├── models.py ├── tests.py └── views.py 2 directories, 10 files [root@center-me modles_me]# |
配置MySQL,连接:
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 |
[root@center-me modles_me]# pwd /script/python/django/modles_me [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr total 4 -rw-r--r-- 1 root root 252 Dec 4 22:03 manage.py drwxr-xr-x 2 root root 70 Dec 4 22:03 modles_me drwxr-xr-x 2 root root 85 Dec 4 22:03 mysql_app [root@center-me modles_me]# [root@center-me modles_me]# vi modles_me/settings.py [root@center-me modles_me]# cat -n modles_me/settings.py | grep -A 14 "DATABASES" 58 DATABASES = { 59 'default': { 60 #'ENGINE': 'django.db.backends.sqlite3', 61 #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 62 63 'ENGINE': 'django.db.backends.mysql', 64 'NAME': 'django', 65 'USER': 'django', 66 'PASSWORD': '*******', 67 'HOST': '10.158.1.96', 68 'PORT': '3306', 69 70 } 71 } 72 [root@center-me modles_me]# |
数据库创建用户以及数据库:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@dg12c1 ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2622567 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> grant all privileges on *.* to django@'10.158.1.99' identified by '****'; Query OK, 0 rows affected (0.07 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.85 sec) MariaDB [(none)]> MariaDB [(none)]> create database django; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> |
配置:models.py
1 2 3 4 5 6 7 8 9 10 11 |
[root@center-me modles_me]# pwd /script/python/django/modles_me [root@center-me modles_me]# [root@center-me modles_me]# cat mysql_app/models.py from django.db import models # Create your models here. class Mysql_testing(models.Model): name = models.CharField(max_length=20) [root@center-me modles_me]# |
在setting.py中,定义应用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@center-me modles_me]# ls manage.py modles_me mysql_app [root@center-me modles_me]# [root@center-me modles_me]# vi modles_me/settings.py [root@center-me modles_me]# cat -n modles_me/settings.py | grep -A 10 "INSTALLED_APPS" 32 INSTALLED_APPS = ( 33 'django.contrib.admin', 34 'django.contrib.auth', 35 'django.contrib.contenttypes', 36 'django.contrib.sessions', 37 'django.contrib.messages', 38 'django.contrib.staticfiles', 39 'mysql_app', 40 ) 41 42 MIDDLEWARE_CLASSES = ( [root@center-me modles_me]# |
在继续下一步之前,先看看我们数据库的状态:
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 |
[root@dg12c1 ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2660932 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> show databases; +-----------------------------+ | Database | +-----------------------------+ | information_schema | | django | | me | | mysql | | mysql_enterprise_monitor_db | | performance_schema | | test | | you | +-----------------------------+ 8 rows in set (0.00 sec) MariaDB [(none)]> MariaDB [(none)]> use django Database changed MariaDB [django]> show tables; Empty set (0.01 sec) MariaDB [django]> |
Django所在的机器上,执行命令:
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 |
[root@center-me modles_me]# pwd /script/python/django/modles_me [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr total 4 -rw-r--r-- 1 root root 252 Dec 4 22:03 manage.py drwxr-xr-x 2 root root 85 Dec 4 22:13 mysql_app drwxr-xr-x 2 root root 108 Dec 4 22:21 modles_me [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr mysql_app/ total 16 -rw-r--r-- 1 root root 0 Dec 4 22:03 __init__.py -rw-r--r-- 1 root root 63 Dec 4 22:03 admin.py -rw-r--r-- 1 root root 60 Dec 4 22:03 tests.py -rw-r--r-- 1 root root 63 Dec 4 22:03 views.py -rw-r--r-- 1 root root 135 Dec 4 22:13 models.py [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr modles_me/ total 20 -rw-r--r-- 1 root root 0 Dec 4 22:03 __init__.py -rw-r--r-- 1 root root 300 Dec 4 22:03 urls.py -rw-r--r-- 1 root root 393 Dec 4 22:03 wsgi.py -rw-r--r-- 1 root root 2187 Dec 4 22:17 settings.py -rw-r--r-- 1 root root 140 Dec 4 22:21 __init__.pyc -rw-r--r-- 1 root root 2263 Dec 4 22:21 settings.pyc [root@center-me modles_me]# [root@center-me modles_me]# django-admin --version 1.6.12 [root@center-me modles_me]# [root@center-me modles_me]# pwd /script/python/django/modles_me [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr total 4 -rw-r--r-- 1 root root 252 Dec 4 22:03 manage.py drwxr-xr-x 2 root root 108 Dec 4 22:21 modles_me drwxr-xr-x 2 root root 121 Dec 4 22:26 mysql_app [root@center-me modles_me]# [root@center-me modles_me]# python manage.py syncdb Creating tables ... Creating table django_admin_log Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_groups Creating table auth_user_user_permissions Creating table auth_user Creating table django_content_type Creating table django_session Creating table mysql_app_mysql_testing You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): yes Username (leave blank to use 'root'): Email address: dkseven_eli@163.com Password: Password (again): Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s) [root@center-me modles_me]# |
上面的方法,对于不同的Django版本来说,是不一样的:
1 2 3 4 5 6 |
# Django 1.6.x 及以下 python manage.py syncdb # Django 1.7 及以上的版本需要用以下命令 python manage.py makemigrations python manage.py migrate |
这时候,再看看MySQL的状态,就跟之前不一样了:
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 |
MariaDB [(none)]> use django Database changed MariaDB [django]> show tables; Empty set (0.01 sec) MariaDB [django]> MariaDB [django]> show tables; +----------------------------+ | Tables_in_django | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_admin_log | | django_content_type | | django_session | | mysql_app_mysql_testing | +----------------------------+ 10 rows in set (0.00 sec) MariaDB [django]> MariaDB [django]> desc mysql_app_mysql_testing; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) MariaDB [django]> MariaDB [django]> select * from mysql_app_mysql_testing; Empty set (0.01 sec) MariaDB [django]> |
Django SHELL操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[root@center-me modles_me]# pwd /script/python/django/modles_me [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr total 4 -rw-r--r-- 1 root root 252 Dec 4 22:03 manage.py drwxr-xr-x 2 root root 108 Dec 4 22:21 modles_me drwxr-xr-x 2 root root 121 Dec 4 22:27 mysql_app [root@center-me modles_me]# [root@center-me modles_me]# python manage.py shell Python 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> >>> from mysql_app.models import Mysql_testing >>> >>> Mysql_testing.objects.create(name="Lenka") <Mysql_testing: Mysql_testing object> >>> >>> Mysql_testing.objects.create(name="Allah") <Mysql_testing: Mysql_testing object> >>> >>> |
来看看数据库的变化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
MariaDB [django]> select * from mysql_app_mysql_testing; Empty set (0.01 sec) MariaDB [django]> MariaDB [django]> select * from mysql_app_mysql_testing; +----+-------+ | id | name | +----+-------+ | 1 | Lenka | | 2 | Allah | +----+-------+ 2 rows in set (0.00 sec) MariaDB [django]> |
用Django SHELL查看上面的值:
先修改代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@center-me modles_me]# pwd /script/python/django/modles_me [root@center-me modles_me]# [root@center-me modles_me]# cat mysql_app/models.py from django.db import models # Create your models here. class Mysql_testing(models.Model): name = models.CharField(max_length=20) def __unicode__(self): return self.name [root@center-me modles_me]# |
来操作一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@center-me modles_me]# python manage.py shell Python 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> >>> from mysql_app.models import Mysql_testing >>> Mysql_testing.objects.get(name="Lenka") <Mysql_testing: Lenka> >>> >>> Mysql_testing.objects.get(name="Allah") <Mysql_testing: Allah> >>> >>> |
如果上面的代码的【unicode】部分没有写,则你无法在Django的SHELL中得到返回结果。
你会得到的是这样的结果:
1 2 3 |
>>> Mysql_testing.objects.get(name="Allah") <Mysql_testing: Mysql_testing object> >>> |
修改:urls.py
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 |
[root@center-me modles_me]# pwd /script/python/django/modles_me [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr total 4 -rw-r--r-- 1 root root 252 Dec 4 22:03 manage.py drwxr-xr-x 2 root root 108 Dec 4 22:21 modles_me drwxr-xr-x 2 root root 121 Dec 4 22:44 mysql_app [root@center-me modles_me]# [root@center-me modles_me]# vi modles_me/urls.py [root@center-me modles_me]# cat modles_me/urls.py from django.conf.urls import patterns, include, url from . import import_data from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'modles_me.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^importdb$', import_data.importdb), ) [root@center-me modles_me]# |
编写代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[root@center-me modles_me]# pwd /script/python/django/modles_me [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr total 4 -rw-r--r-- 1 root root 252 Dec 4 22:03 manage.py drwxr-xr-x 2 root root 121 Dec 4 22:44 mysql_app drwxr-xr-x 2 root root 108 Dec 4 22:48 modles_me [root@center-me modles_me]# [root@center-me modles_me]# vi modles_me/import_data.py [root@center-me modles_me]# cat modles_me/import_data.py # -*- coding: utf-8 -*- from django.http import HttpResponse from mysql_app.models import Mysql_testing def importdb(request): data1 = Mysql_testing(name="Ruth") data1.save() return HttpResponse("<p>The data hase been Inserted.</p>") [root@center-me modles_me]# [root@center-me modles_me]# |
继续下一步之前,先查看数据库的情况:
1 2 3 4 5 6 7 8 9 10 11 |
MariaDB [django]> select * from mysql_app_mysql_testing; +----+-------+ | id | name | +----+-------+ | 1 | Lenka | | 2 | Allah | +----+-------+ 2 rows in set (0.00 sec) MariaDB [django]> MariaDB [django]> |
启动WEB:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@center-me modles_me]# netstat -tupln | grep 8000 [root@center-me modles_me]# [root@center-me modles_me]# ls -ltr total 4 -rw-r--r-- 1 root root 252 Dec 4 22:03 manage.py drwxr-xr-x 2 root root 121 Dec 4 22:44 mysql_app drwxr-xr-x 2 root root 129 Dec 4 22:53 modles_me [root@center-me modles_me]# [root@center-me modles_me]# [root@center-me modles_me]# nohup python manage.py runserver 0.0.0.0:8000 & [1] 13131 [root@center-me modles_me]# nohup: ignoring input and appending output to ‘nohup.out’ [root@center-me modles_me]# [root@center-me modles_me]# netstat -tupln | grep 8000 tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 13134/python [root@center-me modles_me]# |
网页访问:http://10.158.1.99:8000/importdb
再看看数据库的状态:
1 2 3 4 5 6 7 8 9 10 11 |
MariaDB [django]> select * from mysql_app_mysql_testing; +----+-------+ | id | name | +----+-------+ | 1 | Lenka | | 2 | Allah | | 3 | Ruth | +----+-------+ 3 rows in set (0.00 sec) MariaDB [django]> |
读取数据:
改变一下上面的代码:import_data.py
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 |
[root@center-me modles_me]# cat modles_me/import_data.py # -*- coding: utf-8 -*- from django.http import HttpResponse from mysql_app.models import Mysql_testing def importdb(request): #data1 = Mysql_testing(name="Ruth") #data1.save() #return HttpResponse("<p>The data hase been Inserted.</p>") response1 = "" response2 = "" #list = Mysql_testing.objects.all() response2 = Mysql_testing.objects.filter(id=1) response3 = Mysql_testing.objects.get(id=1) #list = Mysql_testing.objects.order_by('name')[0:2] list = Mysql_testing.objects.order_by("id") Mysql_testing.objects.filter(name="Lenka").order_by("id") for item in list: response1 += item.name + " " response = response1 return HttpResponse("<p>"+ response +"</p>") [root@center-me modles_me]# [root@center-me modles_me]# |
重新刷新页面:http://10.158.1.99:8000/importdb
——————————
Done。