Lesson 1
Setting Up Your Django and DRF Environment
Introduction

Welcome to the first lesson of our course on building a TODO app using Django and Django REST Framework (DRF). In this lesson, we'll lay the foundation by setting up our development environment. A solid setup is crucial for effective and efficient development. By the end of this lesson, you will have installed the necessary libraries, created a Django project and app, and configured the basic settings and routing needed to get started.

This initial setup acts as the backbone of any Django-based project. Understanding how to configure and initialize your environment correctly is essential for the smooth development of our TODO app, which we'll build upon in future lessons.

Installing Django and DRF

Before we start creating our project, we need to install Django and DRF. These libraries form the core of our development environment.

To install Django and DRF, open your terminal and run the following command:

Bash
1pip install django djangorestframework

This command installs Django, a high-level Python web framework that encourages rapid development, and Django REST Framework, a powerful and flexible toolkit for building Web APIs.

Execute it in a Python virtual environment to keep your project dependencies isolated. These libraries will come pre-installed in CodeSignal's environment.

Creating a Django Project

Next, we'll create a new Django project. A Django project is a collection of settings for an instance of Django, including database configuration, application-specific settings, and application initialization.

Run the following commands in your terminal:

Bash
1django-admin startproject myproject 2cd myproject

Here's what each command does:

  • django-admin startproject myproject: This command creates a new directory called myproject, which contains the initial project structure.
  • cd myproject: This changes your current directory to myproject.

At this point, your project structure should look like this:

1myproject/ 2 manage.py 3 myproject/ 4 __init__.py 5 settings.py 6 urls.py 7 wsgi.py
Creating a Django App

Now that we have our project, we need to create a Django app. An app is a web application that does something, like a blog system, a database of public records, or, in our case, a TODO app. A project can have multiple apps. For example, you could have apps for authentication system and main website functionality to nicely separate their logic.

Run the following command inside the myproject directory:

Bash
1django-admin startapp myapp

This command creates a new directory called myapp with the following structure:

Bash
1myapp/ 2 __init__.py 3 admin.py 4 apps.py 5 migrations/ 6 __init__.py 7 models.py 8 tests.py 9 views.py
Configuring the Settings

Next, we need to register our new app and DRF in the project's settings. Open myproject/settings.py, and find the INSTALLED_APPS list. Modify it to include myapp and rest_framework:

Python
1INSTALLED_APPS = [ 2 'myapp', # Added our app 3 'rest_framework', # Added REST framework app 4 'django.contrib.admin', 5 'django.contrib.auth', 6 'django.contrib.contenttypes', 7 'django.contrib.sessions', 8 'django.contrib.messages', 9 'django.contrib.staticfiles', 10]

Here's a breakdown of what we did:

  • 'myapp': Registers the myapp application we created.
  • 'rest_framework': Registers DRF so that we can use its features in our project.

Save the file to confirm these changes.

Routing in Django

Finally, we need to configure the URLs to route requests to the appropriate part of our application. Open myproject/urls.py and modify it as follows:

Python
1from django.contrib import admin 2from django.urls import path, include 3 4urlpatterns = [ 5 path('admin/', admin.site.urls), 6 path('api/', include('myapp.urls')), 7]

Here's what we did:

  • Imported the include function, which allows referencing other URL configurations.
  • Added a route for the admin interface ('admin/').
  • Added a route for our app's URLs ('api/').
Creating App Urls

Next, we must create the urls.py file for our app to handle its specific routes.

In the myapp directory, create a new file named urls.py and add the following code:

Python
1from django.urls import path 2from . import views 3 4urlpatterns = [ 5 6]

Here's what we did:

  • Imported path from django.urls and views from the current directory.
  • Created an empty urlpatterns list where you will define app-specific routes in future lessons.

Save the file to confirm these changes.

Running the Server

With the initial setup complete, it's time to run the Django development server to ensure everything is configured correctly.

To start the server, run the following command in your terminal while inside the myproject directory:

Bash
1python manage.py runserver

Also, you can specify the address and the port:

Bash
1python3 manage.py runserver 0.0.0.0:3000

If everything is set up correctly, you should see output similar to this:

1Watching for file changes with StatReloader 2Performing system checks... 3 4System check identified no issues (0 silenced). 5October 01, 2023 - 15:29:15 6Django version 3.2.7, using settings 'myproject.settings' 7Starting development server at http://127.0.0.1:8000/ 8Quit the server with CONTROL-C.

This output indicates that the server is running correctly. The development server listens at http://127.0.0.1:8000/, which you can visit in your web browser to see your Django project's welcome page.

Important Notes:

  • Locally, the server will, by default, run at http://127.0.0.1:8000/, but in the CodeSignal environment, the server will run at http://0.0.0.0:3000/. In CodeSignal, the server will start automatically when you load the task. You don't need to run the python manage.py runserver command manually. However, if you manually shut down the server or if it went offline for any other reason, you should manually start it. We recommend doing it using python3 manage.py runserver 0.0.0.0:3000 to ensure the address and port are the same as the other files in the task expect them to be.
  • Additionally, if you make any changes to the code, the server will automatically restart to reflect those changes. This ensures that your development environment stays in sync with your latest code updates. It works this way in any environment, not only the CodeSignal one.

This automatic setup lets you focus on coding without worrying about manually managing the server lifecycle.

Output

Our project is empty at this point. We will create more pages with functionality in the next lesson. However, now, if you try to open the address of the server in the web browser, you will see Django's 404 page. If we ping the server with Python (for instance, using a requests library), it will return an HTML code for this page. Here is an example of the output that you might see in the tasks of this unit:

1... 2<body> 3 <div id="summary"> 4 <h1>Page not found <span>(404)</span></h1> 5 6 <table class="meta"> 7 <tr> 8 <th>Request Method:</th> 9 <td>GET</td> 10 </tr> 11 <tr> 12 <th>Request URL:</th> 13 <td>http://0.0.0.0:3000/</td> 14 </tr> 15 16 </table> 17 </div> 18...

This signifies that the server has been run successfully, though it has no endpoints that return useful information yet.

Summary and Preparation for Practice

In this lesson, we covered the initial setup for our Django and DRF environment. Here's a quick recap:

  • We installed Django and DRF.
  • Created a Django project and an app within it.
  • Registered the app and DRF in the project settings.
  • Configured URL routing for the project.

Correct setup is crucial and forms the foundation for the development of our TODO app. Up next, you'll practice these concepts through a series of exercises designed to reinforce what you've learned. Ready to dive in? Let's solidify these basics with some hands-on practice!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.