django rest framework 安装与使用


pip install djangorestframework
pip install markdown       # Markdown support for the browsable API.
pip install django-filter

2、 添加rest_framework 到settings



from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url
from cost_app.views import aliyun_amount_item, tc_amount_item, front_all
from django.views import static  # 新增
from django.conf import settings  # 新增
from assets_app.views import trigger_error
from logs_app.views import request_test
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)
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls')),