Configuring a new web service

We added a new Django app to our existing Django project. Use your favorite editor or IDE to check the Python code in the apps.py file within the restful01/drones folder (restful01drones in Windows). The following lines show the code for this file:

from django.apps import AppConfig 
 
 
class DronesConfig(AppConfig): 
    name = 'drones'

The code declares the DronesConfig class as a subclass of the django.apps.AppConfig class that represents a Django application and its configuration. The DronesConfig class just defines the name class attribute and sets its value to 'drones'.

Now, we have to add drones.apps.DronesConfig as one of the installed apps in the restful01/settings.py file that configures settings for the restful01 Django project. I built the previous string by concatenating many values as follows: app name + .apps. + class name, which is, drones + .apps. + DronesConfig.

We already added the rest_framework app to make it possible for us to use the Django REST framework in the restful01/settings.py file. However, in case you decided to create a new Django project from scratch by following all the steps we learned in Chapter 1, Installing the Required Software and Tools, make sure you don't forget to add the rest_framework app.

Open the restful01/settings.py file that declares module-level variables that define the configuration of Django for the restful01 project. We will make some changes to this Django settings file. Locate the lines that assign a strings list to INSTALLED_APPS to declare the installed apps.

Remove the following line from the INSTALLED_APPS strings list. This way, Django won't consider this app anymore:

'toys.apps.ToysConfig', 

Add the following string to the INSTALLED_APPS strings list and save the changes to the restful01/settings.py file:

'drones.apps.DronesConfig',

The following lines show the new code that declares the INSTALLED_APPS strings list with the added line highlighted and with comments to understand what each added string means. The code file for the sample is included in the hillar_django_restful_06_01 folder, in the restful01/settings.py file:

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    # Django REST framework 
    'rest_framework', 
    # Drones application 
    'drones.apps.DronesConfig', 
] 

This way, we have added the drones application to our initial Django project named restful01.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset