RESTful Python是一种使用Python编程语言实现RESTful API的方法。下面是一些常用的Python库和框架,可以用来创建RESTful API:
-
Flask: Flask是一个轻量级的Python Web框架,可以用来创建RESTful API。它具有灵活、易于使用和快速开发的特点。
-
Django REST framework:Django REST framework是一个强大的Web框架,可以用来快速创建RESTful API。它提供了许多功能,例如认证、授权、序列化和视图等。
-
Falcon:Falcon是一个高性能的Python Web框架,可以用来创建RESTful API。它不仅仅是一个Web框架,还提供了许多中间件和扩展,可以帮助你快速构建高性能的API。
-
Pyramid:Pyramid是一个开源的Python Web框架,可以用来创建RESTful API。它支持多种模板语言、路由和中间件,可以帮助你快速开发复杂的Web应用程序和API。
以上是一些常用的Python库和框架,可以用来创建RESTful API。根据自己的需要和技能水平,选择一个适合自己的框架进行开发即可。
安装django rest framework
建立环境
python -m venv venv
目录多了一个venv目录,可以看到里面的文件夹内容
激活venv环境
目录路径前面多(venv) 说明venv 环境激活
安装相应的python 包 django
pip install django 直接就安装完毕
pip install django
Collecting django
Downloading Django-4.2.1-py3-none-any.whl (8.0 MB)
|██████████████████████████████▌ | 7.6 MB 15 kB/s eta 0:00:27
|██████████████████████████████▌ | 7.6 MB 17 kB/s eta 0:00:23
|██████████████████████████████▌ | 7.6 MB 17 kB/s eta 0:00:22
|██████████████████████████████▌ | 7.6 MB 17 kB/s eta 0:00:22
|██████████████████████████████▌ | 7.6 MB 21 kB/s eta 0:00:18
|██████████████████████████████▌ | 7.6 MB 21 kB/s eta 0:00:17
|██████████████████████████████▌ | 7.6 MB 21 kB/s eta 0:00:17
|██████████████████████████████▌ | 7.6 MB 21 kB/s eta 0:00:16
|███████████████████████████████ | 7.7 MB 21 kB/s eta 0:00:16
|███████████████████████████████ | 7.7 MB 26 kB/s eta 0:00:12
|███████████████████████████████ | 7.7 MB 26 kB/s eta 0:00:12
|███████████████████████████████ | 7.7 MB 26 kB/s eta 0:00:12
|███████████████████████████████ | 7.7 MB 29 kB/s eta 0:00:10
|███████████████████████████████ | 7.7 MB 29 kB/s eta 0:00:10
|███████████████████████████████ | 7.7 MB 26 kB/s eta 0:00:11
|███████████████████████████████ | 7.7 MB 26 kB/s eta 0:00:10
|███████████████████████████████ | 7.7 MB 26 kB/s eta 0:00:10
|███████████████████████████████ | 7.8 MB 20 kB/s eta 0:00:12
|███████████████████████████████ | 7.8 MB 20 kB/s eta 0:00:12
|███████████████████████████████ | 7.8 MB 20 kB/s eta 0:00:11
|███████████████████████████████ | 7.8 MB 17 kB/s eta 0:00:12
|███████████████████████████████ | 7.8 MB 17 kB/s eta 0:00:12
|███████████████████████████████ | 7.8 MB 17 kB/s eta 0:00:11
|███████████████████████████████ | 7.8 MB 17 kB/s eta 0:00:10
|███████████████████████████████▌| 7.8 MB 20 kB/s eta 0:00:0
|███████████████████████████████▌| 7.8 MB 20 kB/s eta 0:00:0
|███████████████████████████████▌| 7.8 MB 20 kB/s eta 0:00:0
|███████████████████████████████▌| 7.9 MB 21 kB/s eta 0:00:0
|███████████████████████████████▌| 7.9 MB 21 kB/s eta 0:00:0
|███████████████████████████████▌| 7.9 MB 21 kB/s eta 0:00:0
|███████████████████████████████▌| 7.9 MB 20 kB/s eta 0:00:0
|███████████████████████████████▌| 7.9 MB 20 kB/s eta 0:00:0
|███████████████████████████████▌| 7.9 MB 20 kB/s eta 0:00:0
|████████████████████████████████| 7.9 MB 17 kB/s eta 0:00:0
|████████████████████████████████| 7.9 MB 17 kB/s eta 0:00:0
|████████████████████████████████| 7.9 MB 17 kB/s eta 0:00:0
|████████████████████████████████| 7.9 MB 16 kB/s eta 0:00:0
|████████████████████████████████| 8.0 MB 16 kB/s eta 0:00:0
|████████████████████████████████| 8.0 MB 16 kB/s eta 0:00:0
|████████████████████████████████| 8.0 MB 16 kB/s eta 0:00:0
|████████████████████████████████| 8.0 MB 22 kB/s eta 0:00:0
|████████████████████████████████| 8.0 MB 22 kB/s
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/backports-zoneinfo/
Collecting backports.zoneinfo; python_version < "3.9"
Downloading backports.zoneinfo-0.2.1-cp38-cp38-win_amd64.whl (38 kB)
Collecting sqlparse>=0.3.1
Downloading sqlparse-0.4.4-py3-none-any.whl (41 kB)
|████████████████████████████████| 40 kB 14 kB/s eta 0:00:01
|████████████████████████████████| 41 kB 18 kB/s
Collecting asgiref<4,>=3.6.0
Downloading asgiref-3.7.2-py3-none-any.whl (24 kB)
Collecting tzdata; sys_platform == "win32"
Downloading tzdata-2023.3-py2.py3-none-any.whl (341 kB)
|███████████████████████████████ | 327 kB 17 kB/s eta 0:00:01
|███████████████████████████████▌| 337 kB 17 kB/s eta 0:00:0
|████████████████████████████████| 341 kB 17 kB/s
Collecting typing-extensions>=4; python_version < "3.11"
Downloading typing_extensions-4.6.2-py3-none-any.whl (31 kB)
Installing collected packages: backports.zoneinfo, sqlparse, typing-extensions, asgiref, tzdata,
django
Successfully installed asgiref-3.7.2 backports.zoneinfo-0.2.1 django-4.2.1 sqlparse-0.4.4 typing-extensions-4.6.2 tzdata-2023.3
WARNING: You are using pip version 20.2.3; however, version 23.1.2 is available.
You should consider upgrading via the 'd:\work\pywork\venv\scripts\python.exe -m pip install --upgrade pip' command.
查看django的版本 4.2.1 最新版本
python -m django version
4.2.1
pip install djangorestframework
pip install djangorestframework
Collecting djangorestframework
Downloading djangorestframework-3.14.0-py3-none-any.whl (1.1 MB)
|██████████████████████████████▌ | 1.0 MB 15 kB/s eta 0:00:04
|███████████████████████████████ | 1.0 MB 14 kB/s eta 0:00:03
|███████████████████████████████ | 1.0 MB 14 kB/s eta 0:00:02
|███████████████████████████████▌| 1.0 MB 14 kB/s eta 0:00:0
|████████████████████████████████| 1.1 MB 14 kB/s eta 0:00:0
|████████████████████████████████| 1.1 MB 14 kB/s
Requirement already satisfied: django>=3.0 in d:\work\pywork\venv\lib\site-packages (from djangorestframework) (4.2.1)
Collecting pytz
Downloading pytz-2023.3-py2.py3-none-any.whl (502 kB)
|██████████████████████████████▌ | 481 kB 22 kB/s eta 0:00:01
|███████████████████████████████ | 491 kB 19 kB/s eta 0:00:01
|████████████████████████████████| 501 kB 19 kB/s eta 0:00:0
|████████████████████████████████| 502 kB 19 kB/s
Requirement already satisfied: backports.zoneinfo; python_version < "3.9" in d:\work\pywork\venv\lib\site-packages (from django>=3.0->djangorestframework) (0.2.1)
Requirement already satisfied: tzdata; sys_platform == "win32" in d:\work\pywork\venv\lib\site-packages (from django>=3.0->djangorestframework) (2023.3)
Requirement already satisfied: asgiref<4,>=3.6.0 in d:\work\pywork\venv\lib\site-packages (from django>=3.0->djangorestframework) (3.7.2)
Requirement already satisfied: sqlparse>=0.3.1 in d:\work\pywork\venv\lib\site-packages (from django>=3.0->djangorestframework) (0.4.4)
Requirement already satisfied: typing-extensions>=4; python_version < "3.11" in d:\work\pywork\venv\lib\site-packages (from asgiref<4,>=3.6.0->django>=3.0->djangorestframework) (4.6.2)
Installing collected packages: pytz, djangorestframework
Successfully installed djangorestframework-3.14.0 pytz-2023.3
WARNING: You are using pip version 20.2.3; however, version 23.1.2 is available.
You should consider upgrading via the 'd:\work\pywork\venv\scripts\python.exe -m pip install --upgrade pip' command.
建立pywork工作目录并且建立一个officehouse项目
django-admin startproject officehouse .
目录建立的路径和文件夹
添加一个officehouse_start 为项目app
django-admin startapp officehouse_start
第一层同步数据库
python manage.py migrate
?[36;1mOperations to perform:?[0m
?[1m Apply all migrations: ?[0madmin, auth, contenttypes, sessions
?[36;1mRunning migrations:?[0m
Applying contenttypes.0001_initial...?[32;1m OK?[0m
Applying auth.0001_initial...?[32;1m OK?[0m
Applying admin.0001_initial...?[32;1m OK?[0m
Applying admin.0002_logentry_remove_auto_add...?[32;1m OK?[0m
Applying admin.0003_logentry_add_action_flag_choices...?[32;1m OK?[0m
Applying contenttypes.0002_remove_content_type_name...?[32;1m OK?[0m
Applying auth.0002_alter_permission_name_max_length...?[32;1m OK?[0m
Applying auth.0003_alter_user_email_max_length...?[32;1m OK?[0m
Applying auth.0004_alter_user_username_opts...?[32;1m OK?[0m
Applying auth.0005_alter_user_last_login_null...?[32;1m OK?[0m
Applying auth.0006_require_contenttypes_0002...?[32;1m OK?[0m
Applying auth.0007_alter_validators_add_error_messages...?[32;1m OK?[0m
Applying auth.0008_alter_user_username_max_length...?[32;1m OK?[0m
Applying auth.0009_alter_user_last_name_max_length...?[32;1m OK?[0m
Applying auth.0010_alter_group_name_max_length...?[32;1m OK?[0m
Applying auth.0011_update_proxy_permissions...?[32;1m OK?[0m
Applying auth.0012_alter_user_first_name_max_length...?[32;1m OK?[0m
Applying sessions.0001_initial...?[32;1m OK?[0m
建立超级用户admin和密码123456 输入邮箱为admin@admin.com
python manage.py createsuperuser
Username (leave blank to use 'administrator'): admin
Email address: admin@admin.com
Password:
Password (again):
?[31;1mThis password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
?[0mBypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
在officehouse_start 目录下建立一个序列化的py文件
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')
配置setting和urls.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]
启动服务
python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified some issues:
WARNINGS:
?[33;1m?: (rest_framework.W001) You have specified a default PAGE_SIZE pagination rest_framework setting, without specifying also a DEFAULT_PAGINATION_CLASS.
HINT: The default for DEFAULT_PAGINATION_CLASS is None. In previous versions this was PageNumberPagination. If you wish to define PAGE_SIZE globally whilst defining pagination_class on a per-view basis you may silence this check.?[0m
System check identified 1 issue (0 silenced).
June 01, 2023 - 09:21:48
Django version 4.2.1, using settings 'officehouse.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
升级pip
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.2.3
Uninstalling pip-20.2.3:
Successfully uninstalled pip-20.2.3
Successfully installed pip-23.1.2
pip install django-filter
Collecting django-filter
Downloading django_filter-23.2-py3-none-any.whl (91 kB)
--------------------------------------- 91.9/91.9 kB 76.9 kB/s eta 0:00:00
Requirement already satisfied: Django>=3.2 in d:\work\pywork\venv\lib\site-packages (from django-filter) (4.2.1)
Requirement already satisfied: asgiref<4,>=3.6.0 in d:\work\pywork\venv\lib\site-packages (from Django>=3.2->django-filter) (3.7.2)
Requirement already satisfied: sqlparse>=0.3.1 in d:\work\pywork\venv\lib\site-packages (from Django>=3.2->django-filter) (0.4.4)
Requirement already satisfied: backports.zoneinfo in d:\work\pywork\venv\lib\site-packages (from Django>=3.2->django-filter) (0.2.1)
Requirement already satisfied: tzdata in d:\work\pywork\venv\lib\site-packages (from Django>=3.2->django-filter) (2023.3)
Requirement already satisfied: typing-extensions>=4 in d:\work\pywork\venv\lib\site-packages (from asgiref<4,>=3.6.0->Django>=3.2->django-filter) (4.6.2)
Installing collected packages: django-filter
Successfully installed django-filter-23.2