Django REST Framework

Introduction

From monolithic  to services

Daniel Correa

Web Engineer

@danielcb29

What
Is
Django?

2003

MVT

Model

MVT

Model

Model Definition
=>

MVT

Model

Fields
=>

{

MVT

Model

Methods
=>

{

Model

View

MVT

=>

Model

View

MVT

=>

Model

View

Template

=>

=>

MVT

¿MVT == MVC?

Forms 

forms.py
form.html

Urls

cars/urls.py

And the others...

 ¿MVT
==
Always Server-side rendering ?

Please provide a service to get the car list on my React Native App

After a while...

  • Identify our client type using URL param and return a JSON or HTTP Response according to the case

 

  • Doing the same but using the user agent to check if there is a mobile device or a desktop browser

 

  • Build a separate view that returns just JSON on certain URLs​

But then...

What happens with URLs or out app routing?

What happens with the CBV that already have some logic abstractions?

Even Worse: What happens with the Authentication?, CORS?

...etc

REST!

REpresentational

State

Transfer

REST

Client

Server

HTTP Request

}

Method: GET, POST, PUT, DELETE

Header: Metadata

Endpoint: URL to the source

Payload (optional): Information that client sends to server

REST

Client

Server

HTTP Response

{

Status Code: 20*, 30*, 40*, 50*

Response Data (optional): Commonly  JSON

Response Header: Metadata

Django
REST
Framwork

Warning...

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

REST framework requires the following:

Getting start

pip install djangorestframework
pip install markdown       
pip install django-filter 

In our Python environment we start running the following commands:

1

Getting start

INSTALLED_APPS = (
    ...
    'rest_framework',
)

Then importing the app in our INSTALLED_APPS:

2

Getting start

urlpatterns = [
    ...
    url(r'^<url>/', 
        include('rest_framework.urls'))
]

In case we want to use rest framework Browsable API

3

We are ready to start!

4

How to implement your API

Serializer

How to implement your API

Serializer

=>

View

How to implement your API

Serializer

=>

View

=>

Endpoint

And that's it!, you have an API working from your Django APP

Your client is happy now!

Thanks!
Questions?

@danielcb29

Made with Slides.com