Django rest framework basic introduction and sample code, djangoframework

Source: Internet
Author: User
Tags install django pip install django

Django rest framework basic introduction and sample code, djangoframework

This article focuses on Django rest framework and shares example as follows.

The Django REST framework is a powerful and flexible toolkit for Building Web APIs.

Some reasons you may want to use the REST framework:

  • Web browsing APIs are a great victory for your developers.
  • The authentication policy includes OAuth1a and oau2's packages.
  • Supports the serialization of ORM and non-ORM Data sources.
  • If you do not need more powerful features, you can customize everything-simply use a feature-based general view.
  • Extensive documentation and excellent community support.
  • Used and trusted by Mozilla, Red Hat, Heroku, Eventbrite, and other internationally renowned companies.

Requirements

The REST framework requires the following:

  • Python (2.7, 3.2, 3.3, 3.4, 3.5)
  • Django (1.8, 1.9, 1.10)

The following software packages are optional:

  • Coreapi (1.32.0 +)-supports mode generation.
  • Markdown (2.1.0 +)-supports Markdown for API browsing.
  • Django-filter (1.0.1 +)-filtering supported.
  • Django-cripy-forms-improved HTML display filtering.
  • Django-guardian (1.1.1 +)-supports object-level permissions.

Installation

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

Add 'rest _ framework' to your INSTALLED_APPS setting.

INSTALLED_APPS = (  ...  'rest_framework',)

If you want to use a browsed API, you may also need to add the logon and logout views of the REST framework. Add the following content to your root URL. py file.

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

Note that the URL path can be anything you want, but you must include "rest_framework.urls" in the rest_framework namespace ". You can omit the namespace in Django 1.9 +, and the REST framework will set it for you.

Example

Let's take a look at a simple example of using the REST framework to build an API supported by a simple model.

We will create a read/write API to access the information of our project users.

All global settings of the REST framework API are saved in a single configuration dictionary named REST_FRAMEWORK. First, add the following content to the 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'  ]}

Do not forget to make sure you have added rest_framework to your INSTALLED_APPS.

We are ready to create our API. This is the root urls. py module of our project:

From django. conf. urls import url, includefrom django. contrib. auth. models import Userfrom rest_framework import routers, serializers, viewsets # Serializers defines API representation. Class UserSerializer (serializers. hypervisor modelserializer): class Meta: model = User fields = ('url', 'username', 'email ', 'is _ staff') # ViewSets define view behavior. Class UserViewSet (viewsets. ModelViewSet): queryset = User. objects. all () serializer_class = UserSerializer # vro provides a simple method to automatically determine the URL conf. Router = routers. DefaultRouter () router. register (r 'users', UserViewSet) # use an automatic URL routing to connect to our API. # In addition, we also include the logon URL of the browsed API. Urlpatterns = [url (R' ^ ', include (router. url (R' ^ api-auth/', include ('rest _ framework. urls ', namespace = 'rest _ framework '))

You can now open this API in http: // 127.0.0.1: 8000/browser and view the new "user" API. If you use the logon control in the upper-right corner, you can also add, create, and delete users from the system.

Summary

The above is all about the basic introduction of Django rest framework in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.