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

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.

主站蜘蛛池模板: 邹平县| 同心县| 张家口市| 买车| 安新县| 永年县| 霍邱县| 达拉特旗| 长兴县| 三门县| 高碑店市| 怀化市| 晋中市| 淄博市| 屏南县| 余干县| 阿城市| 东兰县| 华池县| 金坛市| 石家庄市| 长葛市| 榆中县| 抚远县| 郎溪县| 怀集县| 剑阁县| 赤峰市| 博爱县| 门头沟区| 黑水县| 大丰市| 皋兰县| 桃园县| 镶黄旗| 讷河市| 泸定县| 开化县| 昆山市| 西峡县| 宁蒗|