Introduction
DRF framework, the whole process: django-rest framework, rest is the name of the plug-in, the name of the django plug-in is rest, and framework means the framework. DRF is provided as a Django extension application, so we can directly use the existing Django environment without recreating it. (If there is no Django environment, you need to create an environment to install Django first)
Install django related dependencies
- Three-party library
# 创建django骨架
django-admin startproject djangoy
- Start the django service
python manage.py runserver
这是报错: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.
Execute the prompt command. python manage.py migrate
, the function is to rebuild the table structure without affecting the existing data after modifying the model and
start again, no error is found
Settings.py file analysis
When using the django-admin command to create a project, the settings file will be automatically generated, and we can modify it according to the actual needs of the application.
1. BASE_DIR project path
BASE_DIR = Path(__file__).resolve().parent.parent
2.
The variable SECRET_KEY encrypted string is essentially an encrypted salt, which is used to perform Hash processing on various data that needs to be encrypted, such as password reset, form submission, session data, etc. The usual practice is to store it in the system environment variable In, get it by os.getenv(key,default=None)
SECRET_KEY = 'django-insecure-&wzry=5&m94moji)iuu+hz%0yhw_m%e@xpdu&)w8pms)mp6un&'
3. DEBUG switch
DEBUG = True
4. ALLOWED_HOSTS
is used to configure domain names that can access the current site. When DEBUG is configured as False, it is a required item. Set ALLOWED_HOSTS=['*'] to allow all domain names to access
ALLOWED_HOSTS = ['*']
5.
The parameter INSTALLED_APPS configures the path list of App packages that need to be loaded in the current project. Django will add admin (management background), auth (authorization system), and sessions (session system) by default, and you can add or delete configurations according to the needs of the project
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
6. MIDDLEWARE
Middleware list configuration that needs to be loaded in the current project. Similar to the INSTALLED_APPS variable, Django will also add some middleware by default, such as SessionMiddleware for handling sessions, AuthenticationMiddleware for handling authorization verification, and so on. Similarly, configurations can be added or deleted according to the needs of the project
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
7.
The ROOT_URLCONF variable marks the root URL configuration of the current project, which is the entry point of the Django routing system.
8. TEMPLATES
is a list variable used for project template configuration. Each element in the list is a dictionary. A dictionary representing a template engine
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
9. WSGI_APPLICATION
The full Python path to the WSGI application object that Django's built-in server will use
10. DATABASES
This is a dictionary variable that identifies the database configuration of the project
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
The sqlite3 that comes with Django is not suitable for the database of the application project, so here, MySQL is used instead of the default database of the project
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 配置使用的数据库引擎
'NAME': 'django_test', # 数据库的名称
'USER': 'root', # 数据库用户名
'PASSWORD': 'root', # 数据库密码
'HOST': '127.0.0.1', # 数据库服务器地址,这里使用本地开发环境
'PORT': '3306', # 数据库服务器端口号,MySQL默认为3306
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8mb4'
}# 指定mysql语法
}
}
When django introduces the mysql database, an error occurs:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
Modify mysqlclient to pymysql
# 在项目同级APP目录下__init__.py中添加
import pymysql
pymysql.install_as_MySQLdb() # 使用pymysql代替mysqldb连接数据库
# 命令行输入 迁移重置数据库结构
python3 manage.py makemigrations
python3 manage.py migrate
11. AUTH_PASSWORD_VALIDATORS
Django provides some password validators that support pluggable by default, and multiple ones can be configured at one time. Its main purpose is to avoid applying directly through the user's weak password configuration
12.
The two variables LANGUAGE_CODE and TIME_ZONE represent the locale and time zone of the project respectively
# 修改中文和时区
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
13.
After the USE_I18N and USE_L10N web services are built, they can provide services to users in different countries, which requires the application to support internationalization and localization. These two Boolean variables identify whether the current project needs to enable internationalization and localization functions
14. USE_TZ
marks the processing of time zone. If it is set to True, no matter what TIME_ZONE is set, the time stored in the database is UTC time. If the local time of the server is used, just comment it out
15. STATIC_URL
is used to mark the storage location of static resources in the current project
16.
Default type of DEFAULT_AUTO_FIELD primary key
17. REST_FRAMEWORK
DRF framework configuration modification
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': [
"rest_framework.filters.SearchFilter",
"rest_framework.filters.OrderingFilter"
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', # 指定了url参数为page
'PAGE_SIZE': 20
}
initialization
The database migration applied in INSTALLED_APPS
The migrate command of manage.py is used to synchronize the application model definition or modification to the database. The migrate command will check the list of applications configured in INSTALLED_APPS, and iteratively create the required data tables for each application in turn
Django's migration command for the database:
python manage.py makemigrations
python manage.py migrate
For each modification of the table structure definition applied in the future, the makemigrations command needs to be executed again, and Django will regenerate a new database migration file to record the differences between the table structures. The command rule is to add 1 to the serial number of the previous migration file , such as 0002_xxxx, 0003_xxxx. After that, execute the migrate command again to make the new migration file take effect and complete the modification of the table structure definition
Create an administrator account to log in to the django background
manage.py provides the createsuperuser command for creating superusers
python manage.py createsuperuser --username=admin --email=youremail@xx.com
After the current command is executed, you need to repeatedly enter the password twice (set according to your own needs), and finally return "Superuser created successfully" to indicate that the super user has been successfully created
Visit: http://127.0.0.1:8000/admin to enter the background management page
create application
Use the startapp command provided by manage.py to create a video application command
python manage.py startapp video
# video是项目名称
DRF
- install dependencies
- settings file configuration APP
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework', # DRF
'SkrAuto.apps.SkrautoConfig' # 应用
]
DRF framework encapsulation style
# DEMO
from rest_framework.views import APIView # 完成视图
# from rest_framework.generics import GenericAPIView # 二次封装依赖 等同于APIView
from rest_framework.response import Response # 响应数据
from rest_framework.request import Request # 请求数据
from rest_framework.serializers import Serializer # 序列化类
from rest_framework.settings import APISettings
from rest_framework.filters import SearchFilter # 查询过滤器
from rest_framework.pagination import PageNumberPagination # 分页器
from rest_framework.authentication import TokenAuthentication # auth认证
from rest_framework.permissions import IsAuthenticated # 权限认证
from rest_framework.throttling import SimpleRateThrottle # 频率
class Test(APIView):
def get(self, request, *args, **kwargs):
return Response('drf get ok')
# project urls
# django and DRF
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
# path('admin/', admin.site.urls),
path('', include("SkrAuto.urls")),
]
# 应用级视图 views
# django DEMO
class SkrAutoView(GenericAPIView):
def get(self, request):
queryset = UserInfo.objexts.all()
user_list = []
for user in queryset:
user_list.append({
"id": user.id,
"name": user.name,
"sex": user.sex,
"age": user.age,
"phone": user.phone
})
return JsonResponse(user_list, safe=False, json_dumps_params={
"ensure_ascii": False})
# return Response({'code': 200, 'msg': '请求成功'})
def post(self, request):
json_bytes = request.body
json_str = json_bytes.decode()
user_dict = json.loads(json_str)
print(user_dict["name"])
# ORM
user = UserInfo.objects.creat(
name=user_dict.get('name'),
sex=user_dict.get('sex'),
age=user_dict.get('age'),
phone=user_dict.get('phone')
)
return JsonResponse({
"id": user.id,
"name": user.name,
"sex": user.sex,
"age": user.age,
"phone": user.phone
})
def put(self, request, pk):
try:
user = UserInfo.objects.get(pk=pk)
except UserInfo.DoesNotExist:
return Response({
'code': 103, 'msg': '用户不存在'})
# 处理传参
json_bytes = request.body
json_str = json_bytes.decode()
user_dict = json.loads(json_str)
print(user_dict.get("name"))
user.name = user_dict.get("name")
user.save()
return JsonResponse({
"id": user.id,
"name": user.name,
"sex": user.sex,
"age": user.age,
"phone": user.phone
})
def delete(self, request, pk):
try:
user = UserInfo.objects.get(pk=pk)
except UserInfo.DoesNotExist:
return Response({
'code': 103, 'msg': '用户不存在'})
user.delete()
return Response({
'code': 200, 'msg': 'ok'})
# django demo2
# class SkrPostView(GenericAPIView):
#
# def __init__(self):
# super(SkrPostView, self).__init__()
# self.data = {}
#
# def post(self, request):
# c = request.data.get('a') + request.data.get('b')
# self.data['a'] = request.data.get('a')
# self.data['b'] = request.data.get('b')
# self.data['c'] = c
# return Response({'code': 200, 'data': self.data, 'msg': 'ok'})
#
#
# class SkrPutView(GenericAPIView):
#
# def __init__(self):
# super(SkrPutView, self).__init__()
# self.data = {}
#
# def put(self, request, pk):
# # c = request.data.get('a') + request.data.get('b')
# # self.data['a'] = request.data.get('a')
# # self.data['b'] = request.data.get('b')
# # self.data['c'] = c
# try:
# user = UserInfo.objects.get(pk=pk)
# except UserInfo.DoesNotExist:
# return Response({'code': 103, 'msg': '用户不存在'})
# # 参数解析
# json_bytes = request.body
# json_str = json_bytes.decode()
# user_dict = json.loads(json_str)
#
# return Response({'code': 200, 'data': self.data, 'msg': 'ok'})
# DRF DEMO
class SkrViewSet(ModelViewSet):
queryset = UserInfo.objects.all()
serializer_class = SkrSerializers
# 应用级 urls
# django demo
urlpatterns = [
# demo
# path('get/demo/', SkrAutoView.as_view()),
# path('post/demo/', SkrPostView.as_view()),
# re_path(r'get/demo/', views.SkrAutoView.as_view()),
# path('put/demo/'), SkrPutView.as_view(),
# re_path(r'^skr/$', views.SkrAutoView.as_view()), demo
# re_path(r'^skr/(?P<pk>\d+)/$', views.SkrAutoView.as_view()), demo
]
# DRF DEMO
from rest_framework.routers import DefaultRouter, SimpleRouter
from SkrAuto import views
urlpatterns = [ ]
# router = DefaultRouter()
router = SimpleRouter()
router.register(r"skr", views.SkrViewSet)
urlpatterns += router.urls
# models
# ORM模型
# serializers 序列化器 需要新建 serializers.py 文件
from rest_framework import serializers
from django.db import models
from SkrAuto.models import UserInfo
class SkrSerializers(serializers.ModelSerializer):
class Meta:
model = UserInfo
fields = "__all__"
DRF request lifecycle
1. The as_view function of APIView is requested.
2. The as_view of the parent class (django native) is called in the as_view of APIView, and csrf authentication is disabled.
3. The dispatch method of the as_view of the parent class requests the dispatch of APIView
4 . , The method of completing the task is handed over to the request function of the view class for processing, and the response result of the request is obtained and returned to the foreground
service start
# debug
python manage.py runserver
# linux 后台挂载
nohup python manage.py runserver
request example