• drf使用开发接口


    1.创建程序并初始化数据库:

      models.py中的内容:

    from django.db import models
    
    # Create your models here.
    class Category(models.Model):
        """
        文章分类
        """
        name = models.CharField(verbose_name='分类',max_length=32)
    
    class Article(models.Model):
        """
        文章表
        """
        category = models.ForeignKey(verbose_name='分类',to='Category',null=True, blank=True,on_delete=models.PROTECT)  #使用to做模型关联
        title = models.CharField(verbose_name='标题', max_length=32)
        summary = models.CharField(verbose_name='简介', max_length=255)
        content = models.TextField(verbose_name='文章内容')

      运行命令:

      makemigrations 命令

      "D:Program FilesJetBrainsPyCharm 2020.1in unnerw64.exe" D:EvnshulalaScriptspython.exe "D:Program FilesJetBrainsPyCharm 2020.1pluginspythonhelperspycharmdjango_manage.py" makemigrations d:/PycharmProjects/hula       #创建表

      migrate 命令

      "D:Program FilesJetBrainsPyCharm 2020.1in unnerw64.exe" D:EvnshulalaScriptspython.exe "D:Program FilesJetBrainsPyCharm 2020.1pluginspythonhelperspycharmdjango_manage.py" migrate d:/PycharmProjects/hula   #同步表

    2.接口:实现访问接口时,创建一个文章类型

    2.1. 配置url

    from django.contrib import admin
    from django.urls import path
    
    from api import views  #导入api应用中的views.py文件
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('drf/category/', views.DrfCategoryView.as_view()),  # 文章分类的url配置及视图函数
    ]

    2.2. 视图内容

    from rest_framework.views import APIView  #导入rest_framework的APIView
    from rest_framework.response import Response  #导入rest_framework的 Response
    #使用drf中的APIView写相应的get、post、delete、put等响应,使用rest_framework中的response
    #文章分类的View
    class DrfCategoryView(APIView):
        """
        文章分类的drf相关接口
        """
        def get(self,request,*args,**kwargs):  #处理get请求
            data = [
                {
                    "id":1,
                    "title":"震惊了...王洋居然...",
                    "content":"..."
                 },
                {
                    "id": 2,
                    "title": "震惊了...王洋居然...",
                    "content": "..."
                },
                {
                    "id": 3,
                    "title": "震惊了...王洋居然...",
                    "content": "..."
                },
                {
                    "id": 4,
                    "title": "震惊了...王洋居然...",
                    "content": "..."
                },
            ]
    
            return Response(data)   #使用rest_framework的 Response
      def post(self, request, *args, **kwargs):  #处理post请求,加上*args, **kwargs后可以处理url路径后的参数,建议加上
      """
      增加一条分类信息
      :param request:
      :param args:
      :param kwargs:
      :return:
      """
      # name = request.Post.get('...')
      name = 'IT'
      models.Category.objects.create(name=name) #使用create函数创建新加一条数据
       return Response('成功')
       def put(self, request, *args, **kwargs): #处理put请求 
        pass
      def delete(self, request,
    *args, **kwargs): #处理delete请求
        pass

     2.3.访问接口url,如:http://192.168.1.103:8000/drf/category/

      使用postman或者requests或者其他接口访问工具

  • 相关阅读:
    jsp 页面获取当前路径
    html5 页面音频
    微信关于网页授权access_token和普通access_token的区别
    Texlive source
    vscode 快捷键
    vscode setting
    vscode extension 插件管理
    what
    linux manual
    java tool type
  • 原文地址:https://www.cnblogs.com/jingzaixin/p/13580712.html
Copyright © 2020-2023  润新知