Django
Django是一个高级的Python Web框架 ,允许快速开发安全且可维护的网站 。它遵循MVC设计模式,即Model(模型)+View(视图)+ Controller(控制器)设计模式,但在Django中更关注模型 (Model)、模板 (Template)和视图 (Views),称为**MTV模式。**这种架构使得程序员可以方便、快捷地创建高品质、易维护、数据库驱动的应用程序。
MVC架构和MCT架构
MVC模式
![](https://file.jishuzhan.net/article/1754674452584468482/80c6aa2cfcc64fa22ebdb23fa204fd3a.webp)
MVT模式
![](https://file.jishuzhan.net/article/1754674452584468482/ab13d888d5e39ff3a7a8296bc4474bd0.webp)
ORM(翻译官)
ORM,全称为对象关系映射(Object-Relational Mapping),是一种技术,用于将关系数据库中的数据映射到对象中,以便在面向对象编程环境中使用。它提供了一种方法,可以将数据库中的表、记录和字段映射到程序中的对象和属性。
![](https://file.jishuzhan.net/article/1754674452584468482/96d2ac891f6b91c406a5b877c8739702.webp)
Django命令
在windos命令行使用pip命令下载
pip install django==版本号
创建一个空项目
django-admin startproject 项目名
启动项目
python manage.py runserver
创建子项目
python manage.py startapp 子项目名
![](https://file.jishuzhan.net/article/1754674452584468482/98bc55400ccced9099443525c8d5b9e7.webp)
子项目需要注册到主项目中,在主项目下的settings.py文件中
两种添加方式,第一种省事
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 方式一
# '项目名'
# 方式二
'项目名.apps.项目名Config'
]
创建ORM模型类
在子项目中的models.py文件中创建模型类
模型类必须继承model.Model
一个类就是一个数据表,一个对象就是一条数据(元组),一个属性(类属性)就是一个字段
from django.db import models
# Create your models here.
class BookInfo(models.Model):
name = models.CharField(max_length=10)
class PeopleInfo(models.Model):
name = models.CharField(max_length=10)
gender = models.BooleanField()
book = models.ForeignKey(BookInfo, on_delete=models.CASCADE)
创建完模型类之后需要迁移模型(也就是创建表)
第一步生成迁移文件(根据models.py生成sql语句)
python manage.py makemigrations
下图为生成的迁移文件
![](https://file.jishuzhan.net/article/1754674452584468482/28f97e307e652d1718288084979b35a4.webp)
主要内容
# Generated by Django 2.2.5 on 2024-02-05 01:32
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='BookInfo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=10)),
],
),
migrations.CreateModel(
name='PeopleInfo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=10)),
('gender', models.BooleanField()),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.BookInfo')),
],
),
]
第二部执行迁移(根据生成好的sql语句创建数据表)
python manage.py migrate
创建的数据表,默认为sqllite3
可以更改默认的创建数据表格式,在setting.py文件中
![](https://file.jishuzhan.net/article/1754674452584468482/e4df3db0475e6cc038163e80fcb41637.webp)
站点管理
管理界面本地化
在setting.py文件中设置中文
![](https://file.jishuzhan.net/article/1754674452584468482/e1fec0a8270999fb5d9d4b1642429e30.webp)
创建管理员
python manage.py createsuperuser
重置密码
python manage.py changepassword 用户名
注册模型类
在admin.py中注册模型类
![](https://file.jishuzhan.net/article/1754674452584468482/ea3646189062ae06251d936a57e33d98.webp)
发布内容到数据库
![](https://file.jishuzhan.net/article/1754674452584468482/cf0bdc5cc4b950e219e3e748bf0c9bee.webp)
定义视图函数
在view.py中定义对请求的响应动作
![](https://file.jishuzhan.net/article/1754674452584468482/c74ffc93aac8b6f4abe09cdcfe0b2034.webp)
路由匹配
方法一:在urls.py中直接进行配置
![](https://file.jishuzhan.net/article/1754674452584468482/de87b49924fa150c624e88e9106b42c0.webp)
方法二:在子项目里面进行创建一个urls.py配置
![](https://file.jishuzhan.net/article/1754674452584468482/1dd5f9de653a5b90b1c7d5c4291e0c16.webp)
模板的基本使用
需要在setting.py中设置模板的路径
![](https://file.jishuzhan.net/article/1754674452584468482/5e8a27d4262455286f3461face323653.webp)