Django is a Python framework and a tool that makes backend development much easier. In particular, the API using Django Restframework supports the backend of many existing software. This course is for beginners who understand the basic grammar of Python and want to start backend development using Django.
By the way, do you know how to pronounce "Django"? Watch this short video for your first step!
Beginners with basic Python syntax and HTTP knowledge
Who wants to start backend development by using Django
This course is intended for beginners, not for experienced Django developers.
All of the pictures on the instructions are from a Linux OS, but you can do the same on Windows or MAC OS computers.
Set up your environment
Create a Django Project
Setting Up the RESTful API
Assessment Test
Ready to Dive Deep? Let's get started!
If you have not completed all the prerequisites, you can refer to the below to complete them.
Install pip instruction
VSCode in 100 Seconds
Now you are ready to set up a virtual environment.
A Python virtual environment is an isolated workspace for Python projects.
It allows you to install and manage project-specific packages without affecting the global Python installation.
You can easily create the same environment on different machines.
In this instruction, we use the venv command to set up a virtual environment.
1. Navigate to your project directory.
2. Create a virtual environment named "myenv" using the command line.
3.1. Activate the virtual environment and prepare for the next steps. (Linux and Mac)
3.2. For Windows PC
After activating the virtual environment, you can see your virtual environment name as below.
To exit from the virtual environment, you can use the command below.
This section covers the process of installing Django and creating a new Django project. You will learn about the Django project structure and the role of each generated file.
With the virtual environment activated, install Django using pip.
You can check if Django is successfully installed by using this command.
2. Use the django-admin command to generate a new Django project (e.g., "myproject").
Then, a directory with the same name as the project you created will be created.
3. Review the created directory structure, including the manage.py file and the main project folder.
The structure should be like the one below.
Description for key Django files are at the end of this section.
4. Start Server for testing
Change into the project directory.
Then, run server with command below.
Access the server by visiting “localhost:8000” in your web browser.
If you could access your Django server successfully, you can stop server with termination shortcut "Ctr + c".
I've uploaded a demo of these steps to YouTube if you prefer a video explanation.
manage.py
A command-line utility that lets you interact with your Django project. It’s used for running administrative commands such as starting the development server (`runserver`), applying database migrations (`migrate`), running tests, and more. Acts as the entry point for managing your Django project from the terminal, so you do not need to edit this file.
settings.py
This file contains all the configuration settings for your Django project. It includes configurations for the database, installed apps, middleware, templates, static files, security keys, and more. You need to edit this file to change the configuration for the database or installed apps.
urls.py
Defines the URL patterns that map HTTP requests to the corresponding views. It specifies which view should handle a particular URL, essentially acting as a traffic controller for incoming requests. You need to edit to route each endpoint.
For more information, I recommend you to refer to the official documentation of Django. (link)
A RESTful API allows communication between the client and server using HTTP requests and responses, primarily for managing resources. Creating a RESTful API using Django is easy with the Django REST framework.
Install Django REST framework.
You can install Django REST framework on your virtual environment using the below.
You will see "Successfully installed djangorestframework-x.x.x" if it is installed successfully.
You need to add Django REST framework to the "settings.py". I will explain it in 2 steps.
2. Create a Django app (e.g., "myapp") where the API logic will reside.
"myapp" directory is created by doing this command.
3. Add apps to your project.
Update your Django project's settings to include the REST framework and your app.
Edit "myproject/settings.py":
Locate the INSTALLED_APPS section and add 'rest_framework' and your app (e.g., 'myapp'):
4. Create an API View
Now, create a view that will handle API requests. We'll create a simple "Hello World" API endpoint.
Edit "myapp/views.py":
5. Set up URL routing to map your API endpoint.
To expose the API view, you need to create a URL pattern for your app and include it in the main project's URLs.
Edit "myapp/urls.py" (if not exists, create file):
Edit "myproject/urls.py":
This configuration tells Django to route any request to "/api/hello/" to the hello_world view.
6. Test the API by running the Django server and accessing the endpoint in a browser.
Finally, run your Django server.
You can test your API by accessing endpoint "http://localhost:8000/api/hello/" from your browser.
If running correctly, you can see as below.
This is UI provided by Django REST framework to manage your endpoint easily. When you access this endpoint from your frontend software, this endpoint is working as a normal backend API.