zoukankan      html  css  js  c++  java
  • Django REST framework(基础和安装和示例)

    Django REST framework是什么?

    Django应用,能够便于web开发.Django框架

    官网:http://www.django-rest-framework.org/.

    翻译项目:https://q1mi.github.io/Django-REST-framework-documentation/.

    本网页导航

    1.安装

    2.第一个示例

    3.第二个示例

    一丶安装

    安装需要使用pip

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

    注册到Django应用中

    Add 'rest_framework' to your INSTALLED_APPS setting.
    
    INSTALLED_APPS = (
        ...
        'rest_framework',
    )
    

    配置url

    urls.py文件下
    
    urlpatterns = [
        ...
        url(r'^api-auth/', include('rest_framework.urls'))
    ]
    

    二丶第一个示例

    settings.py下
    
    REST_FRAMEWORK = {
        # Use Django's standard `django.contrib.auth` permissions,
        # or allow read-only access for unauthenticated users.
        'DEFAULT_PERMISSION_CLASSES': [
            'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
        ]
    }
    
    urls.py下
    
    from django.conf.urls import url, include
    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 = [
        url(r'^', include(router.urls)),
        url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
    ]
    

    三丶第二个示例

    创建一个Django项目

    # Create the project directory
    mkdir tutorial
    cd tutorial
    
    # Create a virtualenv to isolate our package dependencies locally
    virtualenv env
    source env/bin/activate  # On Windows use `envScriptsactivate`
    
    # Install Django and Django REST framework into the virtualenv
    pip install django
    pip install djangorestframework
    
    # Set up a new project with a single application
    django-admin.py startproject tutorial .  # Note the trailing '.' character
    cd tutorial
    django-admin.py startapp quickstart
    cd ..
    

    迁移数据库

    python manage.py makemigrations
    python manage.py migrate
    

    创建超级用户

    python manage.py createsuperuser
    

    序列化

    应用下,创建serializers.py
    
    from django.contrib.auth.models import User, Group
    from rest_framework import serializers
    
    
    class UserSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = User
            fields = ('url', 'username', 'email', 'groups')
    
    
    class GroupSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = Group
            fields = ('url', 'name')
    

    Views层

    应用中的Views.py下
    
    from django.contrib.auth.models import User, Group
    from rest_framework import viewsets
    from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
    
    
    class UserViewSet(viewsets.ModelViewSet):
        """
        API endpoint that allows users to be viewed or edited.
        """
        queryset = User.objects.all().order_by('-date_joined')
        serializer_class = UserSerializer
    
    
    class GroupViewSet(viewsets.ModelViewSet):
        """
        API endpoint that allows groups to be viewed or edited.
        """
        queryset = Group.objects.all()
        serializer_class = GroupSerializer
    

    配置url

    urls.py下
    
    from django.conf.urls import url, include
    from rest_framework import routers
    from tutorial.quickstart import views
    
    router = routers.DefaultRouter()
    router.register(r'users', views.UserViewSet)
    router.register(r'groups', views.GroupViewSet)
    
    # Wire up our API using automatic URL routing.
    # Additionally, we include login URLs for the browsable API.
    urlpatterns = [
        url(r'^', include(router.urls)),
        url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
    ]
    

    注册rest_framework

    settings.py下
    
    INSTALLED_APPS = (
        ...
        'rest_framework',
    )
    

     

      

  • 相关阅读:
    使用 elementUI 的表单进行查询,表单中只有一个文本框时,回车会自动触发表单的提交事件,导致页面的刷新。
    Vue+elementUI 创建“回到顶部”组件
    elementUI 表格 table 的表头错乱问题
    阿拉伯数字转中文大写(整数)方法
    vue开发 回到顶部操作
    vue-cli 项目中使用 v-chart 及导出 chart 图片
    vue-router路由钩子
    vue随记
    vue中的watch
    Ajax 同步异步互相转换以及区别
  • 原文地址:https://www.cnblogs.com/cangshuchirou/p/9259818.html
Copyright © 2011-2022 走看看