博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
REST framework
阅读量:6419 次
发布时间:2019-06-23

本文共 5314 字,大约阅读时间需要 17 分钟。

REST framework requires the following:

  • Python (2.7, 3.4, 3.5, 3.6, 3.7)
  • Django (1.11, 2.0, 2.1, 2.2)

We highly recommend and only officially support the latest patch release of each Python and Django series.

The following packages are optional:

  • (1.32.0+) - Schema generation support.
  • (2.1.0+) - Markdown support for the browsable API.
  • (1.0.1+) - Filtering support.
  • - Improved HTML display for filtering.
  • (1.1.1+) - Object level permissions support.

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

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

...or clone the project from github.

git clone https://github.com/encode/django-rest-framework

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

Note that the URL path can be whatever you want.

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, includefrom django.contrib.auth.models import Userfrom 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 , 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.

Can't wait to get started? The is the fastest way to get up and running, and building APIs with REST framework.

See the for information on how to clone the repository, run the test suite and contribute changes back to REST Framework.

For support please see the , try the #restframework channel on irc.freenode.net, search , or raise a question on , making sure to include the tag.

For priority support please sign up for a .

For updates on REST framework development, you may also want to follow on Twitter.

If you believe you’ve found something in Django REST framework which has security implications, please do not raise the issue in a public forum.

Send a description of the issue via email to . The project maintainers will then work with you to resolve any issues where required, prior to any public disclosure.

Copyright © 2011-present, . All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

转载于:https://www.cnblogs.com/zpaixx/p/10475530.html

你可能感兴趣的文章
和“C”的再遇
查看>>
一键安装kubernetes 1.13.0 集群
查看>>
RabbitMq的集群搭建
查看>>
spring boot + mybatis 同时访问多数据源
查看>>
URL中汉字转码
查看>>
[转]go正则实例
查看>>
Selector中关于顺序的注意事项
查看>>
小黑小波比.清空<div>标签内容
查看>>
Java中的ExceptionInInitializerError异常及解决方法
查看>>
Spring 注入bean时的初始化和销毁操作
查看>>
java线程同步原理(lock,synchronized)
查看>>
MyEclipse中使用Hql编辑器找不到Hibernate.cfg.xml文件解决方法
查看>>
yRadio以及其它
查看>>
第四节 对象和类
查看>>
闪迪(SanDisk)U盘防伪查询(官方网站)
查看>>
Android onMeasure方法介绍
查看>>
无锁数据结构
查看>>
MySQL的变量查看和设置
查看>>
android onNewIntent
查看>>
XML特殊符号
查看>>