Notes/Django REST Framework Guide/4_REST_Framework_Setup.md

2.1 KiB

REST Framework Setup

API App

When writing backend APIs, you make changes to your app that may not be compatible with older versions.

Examples of these include the following;

  • You changed what should be sent to your login API in order to authenticate with a newer version of your backend
  • When requesting books from your database, a newer version of your API may return a different format (XML instead of JSON)

You should take this into account when building a new project, otherwise users on older versions may encounter errors.

!_resources/4_REST_Framework_Setup/3b993b4fb30752dbd22c157c4c852f82_MD5.jpg

This lets you allow users on older versions to still use your app (v1) while allowing newer users without any overlap (v2)

We will structure your project in the same manner as above. Create a folder named api in your Django project folder besides config

!_resources/4_REST_Framework_Setup/e4b384b960e31b256a53775aa6f61ff2_MD5.jpg

Create the following files inside the api folder

  • __init__.py
  • urls.py

!_resources/4_REST_Framework_Setup/8357583c43ac252db3811fcc35f21564_MD5.jpg Leave __init__.py blank. Instead, open urls.py and add the following code

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

We will then need to point the main config's url file (url.py) to the one we just created. Open the urls.py file in the config folder, replace it with the following

from django.urls import path, include

urlpatterns = [
    path('api/v1/', include('api.urls')),
]

Your setup should then look like this

![[_resources/4_REST_Framework_Setup/8a1e35b34d95be992bd550b32bfe03fa_MD5.jpg]]

![[_resources/4_REST_Framework_Setup/f9a1c18b83ca805afb4662e6ba43eca2_MD5.jpg]]

There is no need to include the api app in your INSTALLED_APPS (config/settings.py)

The next section will handle users and authentication