官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 绥德县| 西丰县| 东丽区| 文安县| 临夏县| 崇仁县| 天柱县| 图木舒克市| 肃北| 德惠市| 吴江市| 兴宁市| 贡嘎县| 蕉岭县| 金阳县| 吉隆县| 广汉市| 平江县| 丘北县| 新邵县| 济南市| 喀喇沁旗| 电白县| 蚌埠市| 类乌齐县| 克山县| 丹东市| 容城县| 涪陵区| 获嘉县| 临汾市| 伊金霍洛旗| 望奎县| 博乐市| 兴业县| 武安市| 天祝| 汉川市| 龙江县| 淅川县| 商洛市|