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.
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:
Bash1pip 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.
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:
Bash1django-admin startproject myproject 2cd myproject
Here's what each command does:
django-admin startproject myproject
: This command creates a new directory calledmyproject
, which contains the initial project structure.cd myproject
: This changes your current directory tomyproject
.
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
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:
Bash1django-admin startapp myapp
This command creates a new directory called myapp
with the following structure:
Bash1myapp/ 2 __init__.py 3 admin.py 4 apps.py 5 migrations/ 6 __init__.py 7 models.py 8 tests.py 9 views.py
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
:
Python1INSTALLED_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 themyapp
application we created.'rest_framework'
: Registers DRF so that we can use its features in our project.
Save the file to confirm these changes.
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:
Python1from 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/'
).
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:
Python1from django.urls import path 2from . import views 3 4urlpatterns = [ 5 6]
Here's what we did:
- Imported
path
fromdjango.urls
andviews
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.
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:
Bash1python manage.py runserver
Also, you can specify the address and the port:
Bash1python3 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 thepython 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 usingpython3 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.
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!