MVC的核心思想就是解耦。html
Django遵循MVC思想,其称为MVT。其中:node
解决python安装包依赖问题python
在虚拟环境里面配置,减小依赖。mysql
Django项目建立git
ORM模型类生产表github
sqlite3是一个小型数据库,常常使用在手机登移动端sql
xxxxxxxxxx
C:\Users\admin>cd Desktop\bncDome\test1
C:\Users\admin\Desktop\bncDome\test1>python manage.py shell
In [3]: from booktest.models import BookInfo
In [6]: from datetime import date
In [4]: b=BookInfo()
# 插入
In [5]: b.btitle = '天龙八部'
In [9]: b.bpub_date = date(1990,1,1)
In [10]: b.save()
# 查找
In [14]: b2=BookInfo.objects.get(id=1)
In [15]: type(b2)
Out[15]: booktest.models.BookInfo
In [16]: b2.btitle
Out[16]: '天龙八部'
# 更新
In [17]: b2.btitle ='笑傲江湖'
In [18]: b2.save()
In [19]: b2.btitle
Out[19]: '笑傲江湖'
#删除
In [23]: b2.delete()
生成迁移文件shell
生成迁移表数据库
(1)本地化语言与时间django
(2)建立管理员
(3)启动服务器
(4)浏览器访问
(5)注册模型类
(6)列表文本显示
(7)自定义管理页面
(1)编写view代码,定义视图函数,返回浏览器的内容
(2)建立urls.py:配置url路由,注意严格匹配开头结尾。地址视图都在这里直接匹配
(3) 项目的url文件配置,包含对应的app中的视图
ur与视图匹配过程
l
(1)根项目下建立模板templates文件夹
(2)配置模板目录
这里面的模板文件为html,而模板中不单单为html。其中支持变量,变量使用两个花括号,;里面是view.py中设置的字典键名。
(3)使用模板文件
注意:使用模板文件中须要以上四个步骤,对以上四步进行封装为my_render遍历高效执行。幸运的是django已经帮咱们封装好了这个方法为render。其中第一个参数为request,第二个是模板路径,第三个参数是变量字典,没有变量则为空
(1)启动项目解决端口号占用,指定端口号
(2)案例介绍
(3)视图编写,先导入模型model类,查找MongoDB数据库的表信息
(4)配置url,记得配置主项目下的urls.py,这个文件一次配置屡次使用
(5)建立模板文件
(6)显示图书信息,主页信息
(7) 在视图里面编写图书管理的详细信息
(8) 设置URL地址以及传参
Django在页面间传参的原理是,在页面url配置时候经过括号里面的组进行传参,后面视图会自动解析组内容进行参数填充。多个参数多个组
(8)建立子页面信息
(1)建立新的项目:django-admin startproject tets2
(2) 建立app: django-admin startapp booktest
(3) setting.py中的INSTALLED_APPS注册应用booktest
(4) setting.py中的DATABASES中配置数据库
xxxxxxxxxx
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME':'bj18',
'USER':'root',
'PASSWORD':'admin',
'HOST':'127.0.0.1',
'PORT':3306,
}
}
(5) 预先安装pip install pymysql,以后在test2中须要加两句话:
(6) 根目录下启动服务: python manage.py runserver
(7) 迁移表:python manage.py makemigrations
(8) mysql数据库同步表:python manage.py migrate
(9) 编写views
xxxxxxxxxx
from django.shortcuts import render
from booktest.models import BookInfo
# Create your views here.
def index(request):
# 1 查询全部信息
books = BookInfo.objects.all()
# 2 使用模板
return render(request,'booktest/index.html',{'books':books})
(10) 设置模板目录DIRS
xxxxxxxxxx
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
(11) 在templates下新建booktest文件夹,而后建立html
(12) 配置urls
xxxxxxxxxx
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('booktest/', include('booktest.urls') )
]
booktest的urls
xxxxxxxxxx
from django.urls import path,include
from booktest import views
urlpatterns = [
path('',views.index,name='index')
]
重定向
xxxxxxxxxx
from django.shortcuts import render,redirect
def create(request):
b = BookInfo()
b.btitle = '流星蝴蝶剑'
b.bpub_date= datetime.date(1990,1,1)
b.save()
# return HttpResponse('ok')
return redirect('index')
(13)mysql日志文件
(14)模型-模型关系:1:1,1:多,多:多
两个类之间有多对多的关系,ManyToManyField定义在哪一个类里面均可以
(15)模型-关联查询
(16)管理器对象
(17)元选项:不会由于app改变影响模型类,而后从新作下迁移
模型类对应的表名不依赖于应用的名字