django安装及环境
C:\Users\dell>pip install django
C:\Users\dell>python
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'3.1.7'
新建项目
C:\Users\dell>django-admin.py startproject HelloWorld
运行项目
C:\Users\dell>cd HelloWorld
C:\Users\dell\HelloWorld> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
March 24, 2021 - 10:38:13
Django version 3.1.7, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
访问项目
管理工具 django-admin.py
C:\Users\dell>django-admin.py
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
项目目录
C:\Users\dell>cd HelloWorld
C:\Users\dell\HelloWorld>tree /F
文件夹 PATH 列表
卷序列号为 235F-8261
C:.
│ db.sqlite3
│ manage.py
│
└─HelloWorld
│ asgi.py
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
│
└─__pycache__
settings.cpython-39.pyc
urls.cpython-39.pyc
wsgi.cpython-39.pyc
__init__.cpython-39.pyc
启动项目,指定ip:port
python manage.py runserver 0.0.0.0:8000
视图
views.py
from django.http import HttpResponse
建应用
C:\Users\dell\HelloWorld>django-admin.py startapp TestModel
python manage.py startapp polls
查版本
C:\Users\dell\HelloWorld\app01>python
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'3.1.7'
C:\Users\dell>python -m django --version
3.1.7
查help
C:\Users\dell\HelloWorld\app01>django-admin help
Type 'django-admin help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
数据迁移
python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001
python manage.py check
python manage.py migrate
交互shell
E:\pycode>pip install virtualenv
Collecting virtualenv
Downloading virtualenv-20.4.4-py2.py3-none-any.whl (7.2 MB)
|████████████████████████████████| 7.2 MB 544 kB/s
Collecting filelock<4,>=3.0.0
Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting distlib<1,>=0.3.1
Downloading distlib-0.3.1-py2.py3-none-any.whl (335 kB)
|████████████████████████████████| 335 kB 939 kB/s
Requirement already satisfied: six<2,>=1.9.0 in d:\program files\python39\lib\site-packages (from virtualenv) (1.15.0)
Collecting appdirs<2,>=1.4.3
Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Installing collected packages: filelock, distlib, appdirs, virtualenv
Successfully installed appdirs-1.4.4 distlib-0.3.1 filelock-3.0.12 virtualenv-20.4.4
E:\pycode>virtualenv newenv_dir
created virtual environment CPython3.9.2.final.0-64 in 6477ms
creator CPython3Windows(dest=E:\pycode\newenv_dir, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\dell\AppData\Local\pypa\virtualenv)
added seed packages: pip==21.0.1, setuptools==56.0.0, wheel==0.36.2
activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
E:\pycode>cd newenv_dir
E:\pycode\newenv_dir>dir
驱动器 E 中的卷没有标签。
卷的序列号是 D688-CA57
E:\pycode\newenv_dir 的目录
2021/04/26 14:59 <DIR> .
2021/04/26 14:59 <DIR> ..
2021/04/26 14:59 42 .gitignore
2021/04/26 14:59 <DIR> Lib
2021/04/26 14:59 292 pyvenv.cfg
2021/04/26 14:59 <DIR> Scripts
2 个文件 334 字节
4 个目录 326,191,599,616 可用字节
E:\pycode\newenv_dir>cd Scripts
E:\pycode\newenv_dir\Scripts>activate
(newenv_dir) E:\pycode\newenv_dir\Scripts>pip install django
python manage.py shell
安装虚拟环境
E:\pycode>pip install virtualenv
Collecting virtualenv
Downloading virtualenv-20.4.4-py2.py3-none-any.whl (7.2 MB)
|████████████████████████████████| 7.2 MB 544 kB/s
Collecting filelock<4,>=3.0.0
Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting distlib<1,>=0.3.1
Downloading distlib-0.3.1-py2.py3-none-any.whl (335 kB)
|████████████████████████████████| 335 kB 939 kB/s
Requirement already satisfied: six<2,>=1.9.0 in d:\program files\python39\lib\site-packages (from virtualenv) (1.15.0)
Collecting appdirs<2,>=1.4.3
Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Installing collected packages: filelock, distlib, appdirs, virtualenv
Successfully installed appdirs-1.4.4 distlib-0.3.1 filelock-3.0.12 virtualenv-20.4.4
E:\pycode>virtualenv newenv_dir # 建一个虚拟环境
created virtual environment CPython3.9.2.final.0-64 in 6477ms
creator CPython3Windows(dest=E:\pycode\newenv_dir, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\dell\AppData\Local\pypa\virtualenv)
added seed packages: pip==21.0.1, setuptools==56.0.0, wheel==0.36.2
activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
E:\pycode>cd newenv_dir
E:\pycode\newenv_dir>dir
驱动器 E 中的卷没有标签。
卷的序列号是 D688-CA57
E:\pycode\newenv_dir 的目录
2021/04/26 14:59 <DIR> .
2021/04/26 14:59 <DIR> ..
2021/04/26 14:59 42 .gitignore
2021/04/26 14:59 <DIR> Lib
2021/04/26 14:59 292 pyvenv.cfg
2021/04/26 14:59 <DIR> Scripts
2 个文件 334 字节
4 个目录 326,191,599,616 可用字节
E:\pycode\newenv_dir>cd Scripts
E:\pycode\newenv_dir\Scripts>activate # 激活虚拟环境
(newenv_dir) E:\pycode\newenv_dir\Scripts>pip install django
数据库的使用
DATABASES = {
'default':
{
'ENGINE': 'django.db.backends.mysql', # 数据库引擎
'NAME': 'dbery', # 数据库名称
'HOST': '192.168.108.133', # 数据库地址,本机 ip 地址 127.0.0.1
'PORT': 3306, # 端口
'USER': 'root', # 数据库用户名
'PASSWORD': 'mypassword', # 数据库密码
}
}
import pymysql
pymysql.install_as_MySQLdb()
数据模型相关
django.db
models
models.Model
verbose_name
Char Field
max_length
makemigrations
migrate
Email Field
Text Field
Integer Field
Date Field
Time Field
auto_now_add
Date Time Field
File Field
Image Field
db_index
unique
default
auto_now_add
auto_now
models. employee.objects.filter(name= "张三").delete()
models.employee.objects.filter(name='tom').update(email="tom2@163.com")
all()
filter()
.filter(name__icontains="tom")
.filter(id__in=[10, 20, 66])
.exclude(id__in=[10, 20, 66])
.filter(id__gt=1, id__lt=10)
.filter(id__range=[1, 66])
.filter(birthday__month=9)
order_by()
.distinct()
values()
values_list()
get()、first()、last()
.count()
dep=models.Foreign Key(to="department",on_delete=models.CASCADE)
group=models.Many To Many Field(to="group")
salary=models.Decimal Field(max_digits=8,decimal_places=2)
info = models.One To OneField(to='employeeinfo',on_delete=models.CASCADE,null=True)
Foreign Key
正向操作是指由存在外键的表通过外键查找关联的数据库表,反向操作指的是由关联表查找存在外键的数据库表。
路由相关
from django.urls import path,include
path('list_employee_old/',list_employee_old),
path()
include()
re_path()
namespace
name
视图
from django.shortcuts import render,redirect,Http Response
from .models import employee,department,group,employeeinfo
Http Response、render、redirect被称为Django的HTTP响应“三剑客
POST.get() GET.get() get_host() get_full_path() COOKIES.get()
pip常用的几个基本命令
(1)安装package:pip install<package>。
(2)升级package:pip install–U<package>。
(3)卸载package:pip uninstall<package>。
(4)列出已经安装的package:pip list。
mysql 统计数据库表数
SELECT COUNT(*) TABLES, table_schema
FROM information_schema.TABLES
WHERE table_schema = 'jira'
GROUP BY table_schema;
数据迁移
$ python manage.py makemigrations polls
$ python manage.py sqlmigrate polls 0001
$ python manage.py migrate