Writing your first Django app
Before starting your first Django project, make sure Python is installed on your system. As of now, the latest version is Python 3.13.2. If Python isn't installed, follow the steps below to get started.
Python installation
Download Python from the official site: https://www.python.org/downloads/.
You should be directed to the download page. Scroll down until you see the Files section. Since I am using Windows, I will select the recommended installer. Click on it and wait for the download to complete.
For LINUX users, the process of installing Python can vary depending on the specific distribution you are using. Generally, most Linux distributions come with Python pre-installed, but it's important to verify the version to ensure you have the latest one. To do this, open your terminal and type
python3 --version
orpython --version
to check the installed version.If you find that your version is outdated or Python is not installed, you can search online for installation instructions tailored to your distribution. For example, Ubuntu users can typically install Python using the command
sudo apt-get install python3
, while Fedora users might usesudo dnf install python3
. The installation process is usually straightforward and quick, allowing you to get started with your Django project without much delay.
Double-click the installer after downloading, and you should see the window below, don't mind the version difference, yours should be 3.13.2. Click "Install Now."
Before installing, make sure to check the option to add Python to your PATH. This will add Python to your Windows environment variables.
After that, click "Install Now," and Python will be installed.
Now that you have Python installed, open the command line to confirm that Python 3 is installed.
We can now start working with Django.
Remember to refer to the documentation as we go through this course.
Django installation
I forgot to mention that you can use any code editor you prefer. In this guide, I chose to use VSCode. You can check it out if you haven't installed it yet. VSCode
Create a Django folder, where our app will sit. Name it anything you want. I’ll name mine Django. Open the folder with your editor.
Using vs code editor I can open the terminal from where I will run all my commands.
You should have something that looks like this. Notice that I have created another project called lesson001. You can create this folder as well for our first lesson.
In the terminal, we need to check our Python version and start a virtual environment for our project.
A Python virtual environment is an isolated environment that allows you to manage and install dependencies for a specific project without affecting the global Python installation. It's very useful when you have multiple projects with different dependencies.
Our first steps;
cd to the lesson001 folder
cd .\lesson001\
check the Python version to be sure Python is installed
py --version
create the virtual environment
py -m venv venv
the last venv is the name of the virtual environment, you can name it to anything you want.Activate the virtual environment by typing
.\venv\Scripts\activate
into the terminal and pressing enter.You should now see a green "venv" in brackets, indicating that your virtual environment is activated.
You can deactivate it anytime using the deactivate command.
Our lesson001 folder now contains some files, but don't worry about them for now. Let's install Django.
Install Django by typing
pip install Django
into your terminal. This will download and install some packages.Let’s install our first project using
django-admin
the command. Typedjango-admin startproject myapp
You can name the project anything you want.
Our lesson001 folder now contains a myapp folder.
Navigate into the myapp folder using
cd myapp
.Use
ls
ordir
to view the files inside the folder. You should see the manage.py file, which we'll use to run our project.Type
py manage.py runserver
to start our project.As you can see, the project is running correctly. You can now click on http://127.0.0.1:8000/ or copy and paste it into your browser.
Congratulations, you have just created your very first app!
I know this is already quite long, but you've just tackled the hardest parts. From here on, it will be smoother sailing. Before our next article, make sure you practice this session until you master it. Experiment with it, and be sure to read ahead as well.