码迷,mamicode.com
首页 > 其他好文 > 详细

04: DjangoRestFramework使用

时间:2019-06-07 13:05:39      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:pip   install   work   def   res   include   none   cbv   support   

1.1 DjangoRestFramework基本使用

  1、回顾CBV基本使用

技术图片
from django.contrib import admin
from django.urls import path,re_path,include

urlpatterns = [
    path(‘admin/‘, admin.site.urls),
    re_path(r‘users/‘,include((‘users.urls‘, ‘users‘), namespace=‘users‘))
]
urls.py
技术图片
from django.contrib import admin
from django.urls import path,re_path,include

urlpatterns = [
    path(‘admin/‘, admin.site.urls),
    re_path(r‘users/‘,include((‘users.urls‘, ‘users‘), namespace=‘users‘))
]
users/urls.py
技术图片
import json
from django.shortcuts import render,HttpResponse
from django.views import View

class HomeView(View):

    def dispatch(self, request, *args, **kwargs):
        return super(HomeView, self).dispatch(request, *args, **kwargs)

    def get(self, request):
        return HttpResponse(‘get‘)

    def post(self, request):
        return HttpResponse(‘post‘)
users/views.py

  2、安装DjangoRestFramework

pip install djangorestframework==3.9.2
pip install markdown==3.0.1                  # Markdown support for the browsable API.
pip install django-filter==2.1.0             # Filtering support

  3、DjangoRestFramework 基本使用

技术图片
urlpatterns = [
    url(r‘^users‘, Users.as_view()),
]
urls.py
技术图片
from django.views import View
from django.http import JsonResponse
 
class Users(View):
    def get(self, request, *args, **kwargs):
        result = {
            ‘status‘: True,
            ‘data‘: ‘response data‘
        }
        return JsonResponse(result, status=200)
 
    def post(self, request, *args, **kwargs):
        result = {
            ‘status‘: True,
            ‘data‘: ‘response data‘
        }
        return JsonResponse(result, status=200) 
views.py

 1.2 drf认证模块

 

 

 

 

 

 

 

1111

04: DjangoRestFramework使用

标签:pip   install   work   def   res   include   none   cbv   support   

原文地址:https://www.cnblogs.com/xiaonq/p/10987889.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!