百木园-与人分享,
就是让自己快乐。

Django-rest-framework开发api接口

django-rest-framework开发api接口

(1) 创建django项目drfdemo1并且创建一个名为app的应用

django-admin startproject drfdemo1
python manage.py startapp app

(2) 安装django-rest-framework

pip install djangorestframework

(3) 在settings.py的INSTALLED_APPS注册,并且在INSTALLED_APPS和MIDDLEWARE配置属性中将部分django自带功能去除

INSTALLED_APPS = [
    # \'django.contrib.admin\',
    \'django.contrib.auth\',
    \'django.contrib.contenttypes\',
    \'django.contrib.sessions\',
    # \'django.contrib.messages\',
    # \'django.contrib.staticfiles\',
    \'app\',
    \'rest_framework\',
]

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\',
]

上述配置保留了Auth认证系统和Session功能,当django运行时只会加载Auth认证系统和Session功能,这样能减少django占用服务器的系统资源,除此之外还要注释掉配置属性TEMPLATES和STATIC_URL,因为API接口无需使用模板功能和静态资源

(4) 路由配置

在drfdemo1的urls.py中编写以下内容:

from django.urls import path, include

urlpatterns = [
    path(\'api/\',include(\'app.urls\')),
]

在app里创建urls.py,并写入如下内容:

from django.urls import path
from .views import *

urlpatterns = [
    path(\'students/\',students.as_view())
]

(5) 模型

from django.db import models


class studentsInfo(models.Model):
    name = models.CharField(max_length=12, verbose_name=\'姓名\')
    sex = models.CharField(max_length=1, verbose_name=\'性别\')
    age = models.IntegerField(verbose_name=\'年龄\')
    classroom = models.CharField(max_length=10, verbose_name=\'教室\')
    info = models.CharField(max_length=100, verbose_name=\'个人简介\')

(6) 在app里创建serializer.py,内容如下:

from rest_framework import serializers
from .models import *


class studentSerializer(serializers.ModelSerializer):

    class Meta:
        model = studentsInfo
        fields = \"__all__\"

(7) 视图类编写

from .models import *
from django.http import JsonResponse
from .serializer import studentSerializer
from rest_framework.views import APIView


class students(APIView):
    def get(self, request):
        data = studentsInfo.objects.all()
        serializer = studentSerializer(instance=data,many=True)
        return JsonResponse(serializer.data,safe=False)

(8) 执行数据迁移

python manage.py makemigrations
python manage.py migrate

迁移之后插入测试数据

(9) 启动项目

python manage.py runserver 8000

(10) 查看返回的数据

访问127.0.0.1/api/students,结果如下:

[
    {
        \"id\": 1,
        \"name\": \"小明\",
        \"sex\": \"男\",
        \"age\": 22,
        \"classroom\": \"301\",
        \"info\": \"这个人很懒,什么也没有留下\"
    },
    {
        \"id\": 2,
        \"name\": \"小红\",
        \"sex\": \"女\",
        \"age\": 18,
        \"classroom\": \"301\",
        \"info\": \"这个人很懒,什么也没有留下\"
    },
    {
        \"id\": 3,
        \"name\": \"张三\",
        \"sex\": \"男\",
        \"age\": 25,
        \"classroom\": \"307\",
        \"info\": \"这个人很懒,什么也没有留下\"
    },
    {
        \"id\": 4,
        \"name\": \"李四\",
        \"sex\": \"男\",
        \"age\": 24,
        \"classroom\": \"308\",
        \"info\": \"这个人很懒,什么也没有留下\"
    }
]


来源:https://www.cnblogs.com/minqiliang/p/16586159.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » Django-rest-framework开发api接口

相关推荐

  • 暂无文章