api

Posted 羊小羚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了api相关的知识,希望对你有一定的参考价值。

Installation

Install using pip, including any optional packages you want...

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

...or clone the project from github.

git clone [email protected]:encode/django-rest-framework.git

Add ‘rest_framework‘ to your INSTALLED_APPS setting.

INSTALLED_APPS = (
    ...
    ‘rest_framework‘,
)

If you‘re intending to use the browsable API you‘ll probably also want to add REST framework‘s login and logout views. Add the following to your root urls.py file.

urlpatterns = [
    ...
    url(r‘^api-auth/‘, include(‘rest_framework.urls‘, namespace=‘rest_framework‘))
]

Note that the URL path can be whatever you want, but you must include ‘rest_framework.urls‘ with the ‘rest_framework‘ namespace. You may leave out the namespace in Django 1.9+, and REST framework will set it for you.

Example

Let‘s take a look at a quick example of using REST framework to build a simple model-backed API.

We‘ll create a read-write API for accessing information on the users of our project.

Any global settings for a REST framework API are kept in a single configuration dictionary named REST_FRAMEWORK. Start off by adding the following to your settings.py module:

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‘
    ]
}

Don‘t forget to make sure you‘ve also added rest_framework to your INSTALLED_APPS.

We‘re ready to create our API now. Here‘s our project‘s root urls.py module:

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‘))
]

You can now open the API in your browser at http://127.0.0.1:8000/, and view your new ‘users‘ API. If you use the login control in the top right corner you‘ll also be able to add, create and delete users from the system.

以上是关于api的主要内容,如果未能解决你的问题,请参考以下文章

Android 插件化VirtualApp 源码分析 ( 目前的 API 现状 | 安装应用源码分析 | 安装按钮执行的操作 | 返回到 HomeActivity 执行的操作 )(代码片段

微信小程序代码片段

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段

如何在地图片段 API v2 布局顶部添加按钮

从Snipplr获取一个代码片段

# Java 常用代码片段