DRF官方网站:
https://www.django-rest-framework.org/

官方的安装文档:
https://www.django-rest-framework.org/#installation

首先,在PyCharm中新建一个项目;和普通的Django项目的创建没什么区别

然后,打开终端:

PS G:\PyCharm_data\djangoProject_Akashi> pwd

Path
----
G:\PyCharm_data\djangoProject_Akashi


PS G:\PyCharm_data\djangoProject_Akashi>
PS G:\PyCharm_data\djangoProject_Akashi> dir


    目录: G:\PyCharm_data\djangoProject_Akashi


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2022/2/24     17:05                .idea
d-----         2022/2/24     14:58                djangoProject_Akashi
d-----         2022/2/24     14:53                templates
d-----         2022/2/24     14:53                venv
-a----         2022/2/24     14:56         131072 db.sqlite3
-a----         2022/2/24     14:53            698 manage.py


PS G:\PyCharm_data\djangoProject_Akashi>
PS G:\PyCharm_data\djangoProject_Akashi> pip list
Package    Version
---------- -------
asgiref    3.5.0
Django     4.0.2
pip        21.1.3
PyMySQL    1.0.2
setuptools 57.4.0
sqlparse   0.4.2
tzdata     2021.5
wheel      0.36.2
WARNING: You are using pip version 21.1.3; however, version 22.0.3 is available.
You should consider upgrading via the 'G:\PyCharm_data\djangoProject_Akashi\venv\Scripts\python.exe -m pi
p install --upgrade pip' command.
PS G:\PyCharm_data\djangoProject_Akashi>
PS G:\PyCharm_data\djangoProject_Akashi> pip install djangorestframework
Collecting djangorestframework
  Downloading djangorestframework-3.13.1-py3-none-any.whl (958 kB)
     |████████████████████████████████| 958 kB 15 kB/s
Requirement already satisfied: django>=2.2 in g:\pycharm_data\djangoproject_akashi\venv\lib\site-packages
 (from djangorestframework) (4.0.2)
Collecting pytz
  Using cached pytz-2021.3-py2.py3-none-any.whl (503 kB)
Requirement already satisfied: asgiref<4,>=3.4.1 in g:\pycharm_data\djangoproject_akashi\venv\lib\site-pa
ckages (from django>=2.2->djangorestframework) (3.5.0)
Requirement already satisfied: sqlparse>=0.2.2 in g:\pycharm_data\djangoproject_akashi\venv\lib\site-pack
ages (from django>=2.2->djangorestframework) (0.4.2)
Requirement already satisfied: tzdata in g:\pycharm_data\djangoproject_akashi\venv\lib\site-packages (fro
m django>=2.2->djangorestframework) (2021.5)
Installing collected packages: pytz, djangorestframework
Successfully installed djangorestframework-3.13.1 pytz-2021.3
WARNING: You are using pip version 21.1.3; however, version 22.0.3 is available.
You should consider upgrading via the 'G:\PyCharm_data\djangoProject_Akashi\venv\Scripts\python.exe -m pi
p install --upgrade pip' command.
PS G:\PyCharm_data\djangoProject_Akashi> pip install markdown
Collecting markdown
  Downloading Markdown-3.3.6-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 218 kB/s
Collecting importlib-metadata>=4.4
  Downloading importlib_metadata-4.11.1-py3-none-any.whl (17 kB)
Collecting zipp>=0.5
  Downloading zipp-3.7.0-py3-none-any.whl (5.3 kB)
Installing collected packages: zipp, importlib-metadata, markdown
Successfully installed importlib-metadata-4.11.1 markdown-3.3.6 zipp-3.7.0
WARNING: You are using pip version 21.1.3; however, version 22.0.3 is available.
You should consider upgrading via the 'G:\PyCharm_data\djangoProject_Akashi\venv\Scripts\python.exe -m pi
p install --upgrade pip' command.
PS G:\PyCharm_data\djangoProject_Akashi> pip install django-filter
Collecting django-filter
  Downloading django_filter-21.1-py3-none-any.whl (81 kB)
     |████████████████████████████████| 81 kB 291 kB/s
Requirement already satisfied: Django>=2.2 in g:\pycharm_data\djangoproject_akashi\venv\lib\site-packages
 (from django-filter) (4.0.2)
Requirement already satisfied: tzdata in g:\pycharm_data\djangoproject_akashi\venv\lib\site-packages (fro
m Django>=2.2->django-filter) (2021.5)
Requirement already satisfied: asgiref<4,>=3.4.1 in g:\pycharm_data\djangoproject_akashi\venv\lib\site-pa
ckages (from Django>=2.2->django-filter) (3.5.0)
Requirement already satisfied: sqlparse>=0.2.2 in g:\pycharm_data\djangoproject_akashi\venv\lib\site-pack
ages (from Django>=2.2->django-filter) (0.4.2)
Installing collected packages: django-filter
Successfully installed django-filter-21.1
WARNING: You are using pip version 21.1.3; however, version 22.0.3 is available.
You should consider upgrading via the 'G:\PyCharm_data\djangoProject_Akashi\venv\Scripts\python.exe -m pi
p install --upgrade pip' command.
PS G:\PyCharm_data\djangoProject_Akashi>
PS G:\PyCharm_data\djangoProject_Akashi> pip list
Package             Version
------------------- -------
asgiref             3.5.0
Django              4.0.2
django-filter       21.1
djangorestframework 3.13.1
importlib-metadata  4.11.1
Markdown            3.3.6
pip                 21.1.3
PyMySQL             1.0.2
pytz                2021.3
setuptools          57.4.0
sqlparse            0.4.2
tzdata              2021.5
wheel               0.36.2
zipp                3.7.0
WARNING: You are using pip version 21.1.3; however, version 22.0.3 is available.
You should consider upgrading via the 'G:\PyCharm_data\djangoProject_Akashi\venv\Scripts\python.exe -m pi
p install --upgrade pip' command.
PS G:\PyCharm_data\djangoProject_Akashi>

然后,在Django的配置文件【settings.py】的【INSTALLED_APPS】中添加【’rest_framework’】,具体如下:

INSTALLED_APPS = [
    # 默认
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # 额外
    'rest_framework',

    # 自定义

]

然后,修改Django的路由:文件【urls.py】

"""djangoProject_Akashi URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.urls import path, include
from django.contrib import admin
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets

# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'email', 'is_staff']

# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

然后访问:
User List – Django REST framework

可以看到,确实通过RESTful API的方式,添加成功了。


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

隐藏
变装