Reader small image

You're reading from  Building RESTful Python Web Services

Product typeBook
Published inOct 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786462251
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Gaston C. Hillar
Gaston C. Hillar
author image
Gaston C. Hillar

Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a Bachelor's degree in computer science (graduated with honors) and an MBA. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was a senior contributing editor at Dr. Dobb's, and has written more than a hundred articles on software development topics. He has received the prestigious Intel Black Belt Software Developer award eight times. He has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar

Right arrow

Setting up the virtual environment with Django REST framework


We have created and activated a virtual environment. It is time to run many commands that will be the same for either macOS, Linux or Windows. Now, we must run the following command to install the Django Web framework:

pip install django

The last lines of the output will indicate that the django package has been successfully installed. Take into account that you may also see a notice to upgrade pip.

Collecting django
Installing collected packages: django
Successfully installed django-1.10

Now that we have installed Django Web framework, we can install Django REST framework. We just need to run the following command to install this package:

pip install djangorestframework

The last lines for the output will indicate that the djangorestframework package has been successfully installed:

Collecting djangorestframework
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.3.3

Go to the root folder for the virtual environment-Django01. In macOS or Linux, enter the following command:

cd ~/PythonREST/Django01

In Windows, enter the following command:

cd /d %USERPROFILE%\PythonREST\Django01

Run the following command to create a new Django project named gamesapi. The command won't produce any output:

django-admin.py startproject gamesapi

The previous command created a gamesapi folder with other sub-folders and Python files. Now, go to the recently created gamesapi folder. Just execute the following command:

cd gamesapi

Then, run the following command to create a new Django app named games within the gamesapi Django project. The command won't produce any output:

python manage.py startapp games

The previous command created a new gamesapi/games sub-folder, with the following files:

  • __init__.py

  • admin.py

  • apps.py

  • models.py

  • tests.py

  • views.py

In addition, the gamesapi/games folder will have a migrations sub-folder with an __init__.py Python script. The following diagram shows the folders and files in the directory trees starting at the gamesapi folder:

Let's check the Python code in the apps.py file within the gamesapi/games folder. The following lines shows the code for this file:

from django.apps import AppConfig 
 
 
class GamesConfig(AppConfig): 
    name = 'games' 

The code declares the GamesConfig class as a subclass of the django.apps.AppConfig class that represents a Django application and its configuration. The GamesConfig class just defines the name class attribute and sets its value to 'games'. We have to add games.apps.GamesConfig as one of the installed apps in the gamesapi/settings.py file that configures settings for the gamesapi Django project. We built the preceding string as follows-app name + .apps. + class name, which is, games + .apps. + GamesConfig. In addition, we have to add the rest_framework app to make it possible for us to use Django REST Framework.

The gamesapi/settings.py file is a Python module with module-level variables that define the configuration of Django for the gamesapi project. We will make some changes to this Django settings file. Open the gamesapi/settings.py file and locate the following lines that specify the strings list that declares the installed apps:

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
] 

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

  • 'rest_framework'

  • 'games.apps.GamesConfig'

The following lines show the new code that declares the INSTALLED_APPS strings list with the added lines highlighted. The code file for the sample is included in the restful_python_chapter_01_01 folder:

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', 
    # Games application 
    'games.apps.GamesConfig', 
] 

This way, we have added Django REST Framework and the games application to our initial Django project named gamesapi.

Previous PageNext Page
You have been reading a chapter from
Building RESTful Python Web Services
Published in: Oct 2016Publisher: PacktISBN-13: 9781786462251
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Gaston C. Hillar

Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a Bachelor's degree in computer science (graduated with honors) and an MBA. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was a senior contributing editor at Dr. Dobb's, and has written more than a hundred articles on software development topics. He has received the prestigious Intel Black Belt Software Developer award eight times. He has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar