
Welcome back! In Part 1, we introduced Django and our goal of building a simple blog. Now, in Chapter 2, we’re going to get everything installed on your computer so you can start coding. I’ll explain each step as if you’ve never used a terminal or installed software before, so no worries if this is all brand-new!
1. Why We Need a “Virtual Environment”
Before we install Django, let’s talk about virtual environments. A virtual environment is like a separate box on your computer where you can put specific versions of Python and Django for this project—so they don’t mix up with any other Python projects you might have later. Think of it as a sandbox: whatever we install here stays in this box.
Why this matters:
- Keeps your main system clean
- Prevents version conflicts (one project needs Django 4, another needs Django 3)
- Makes your project easy to share with others (they can recreate the same setup)
2. Installing Python
Check if Python Is Already Installed
- Open your terminal (macOS/Linux) or Command Prompt (Windows):
- Windows: Press ⊞ Win (Windows key), type cmd, and press Enter.
- macOS: Open Finder → Applications → Utilities → Terminal.
- Linux: Look for “Terminal” in your applications menu.
- Type:
python --version
or, if that doesn’t work:
python3 --version
- Read the output:
- If you see something like
Python 3.10.5
You have Python 3 installed. - If you see an error or a Python 2.x version, you’ll need to install Python 3.
- If you see something like
Installing Python (if needed)
- Windows:
- Go to https://www.python.org/downloads/windows/
- Download the latest Windows installer for Python 3.
- Run it, check “Add Python to PATH”, then click “Install Now.”
- macOS:
macOS often comes with Python 2, so it’s safest to install Python 3 via the official installer:- Go to https://www.python.org/downloads/mac-osx/
- Download the “macOS 64-bit installer” for Python 3.
- Run it and follow the prompts.
- Linux:
Most Linux distributions let you install via the package manager. For example, on Ubuntu:sudo apt update sudo apt install python3 python3-venv python3-pip
3. Creating a Virtual Environment
Once Python 3 is installed, we’ll create a virtual environment for our Django project.
- Navigate to your projects folder (or create one):
cd ~ # Goes to your home folder mkdir django-blog # Makes a folder named “django-blog” cd django-blog # Moves into that folder
- Create the virtual environment by typing:
python3 -m venv venv
- Here,
-m venv venv
tells Python to use its built-in “venv” module to make a folder namedvenv
.
- Here,
- Activate the virtual environment:
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
- Windows:
- Check you’re inside the virtual environment:
- Your prompt should now start with
(venv)
. - Any Python or pip commands you run will be contained inside this sandbox.
- Your prompt should now start with
Note: To leave the virtual environment later, simply type
deactivate
and press Enter.
4. Installing Django
With the virtual environment active, let’s install Django itself.
- Ensure pip is up to date (pip is Python’s package installer. We will use it to install other packages in the future):
pip install --upgrade pip
- Install Django by typing:
pip install django
- Verify the installation:
django-admin --version
You should see a version number (e.g.,
4.2.1
). If so, congrats—Django is ready!
5. Creating Your First Django Project
Django provides a command to start a new project with the basic structure already in place.
- In your
django-blog
folder (and with(venv)
active), run:django-admin startproject mysite .
startproject
creates the folder structure.mysite
is the internal name of your project (you can change it).- The final
.
tells Django to place files in the current directory instead of making a new subfolder.
- List your files with:
(venv) mac@macs-MacBook-Pro ls
You should see:
manage.py mysite env
- manage.py: A command-line utility for administrative tasks (running the server, migrations, etc.).
- mysite/: A folder containing settings, URLs, and WSGI configuration.
6. Running the Development Server
Django comes with a lightweight web server for development. Let’s see our project live!
- Apply initial migrations (this sets up the default database tables Django needs):
python manage.py migrate
- Migrations are Django’s way of synchronising your Python data models with the database.
(venv) mac@macs-MacBook-Pro django-blog % python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying auth.0012_alter_user_first_name_max_length... OK Applying sessions.0001_initial... OK
- Run the server:
python manage.py runserver
- Open your browser and go to
http://127.0.0.1:8000/
(or whatever address the terminal shows).- You should see the “Welcome to Django” page confirming everything works.
Tip: If you get an error like “Port 8000 is already in use,” try:
python manage.py runserver 8001
Then visit
http://127.0.0.1:8001/
instead.
7. Quick Tour of the Project Structure
Now that it’s running, let’s peek at what Django created:
django-blog/
├── manage.py
└── mysite/
├── __init__.py
├── settings.py # Configuration (database, apps, static files)
├── urls.py # URL routing for the project
├── asgi.py # For asynchronous servers (you can ignore for now)
└── wsgi.py # For WSGI servers (ignore for now)
- manage.py runs commands like
runserver
,migrate
, and eventuallystartapp
. - settings.py is where you configure your database, installed apps, time zone, and more.
- urls.py defines which web addresses map to which pieces of code.
- init.py files tell Python that these folders are packages (you don’t need to worry about them yet).
8. Next Steps & Troubleshooting
- Can’t activate venv?
- On macOS/Linux, ensure you typed
source venv/bin/activate
. - On Windows, use
venv\Scripts\activate
.
- On macOS/Linux, ensure you typed
- Command not found errors?
- Make sure your terminal is inside the
django-blog
folder. - Ensure
(venv)
appears in your prompt.
- Make sure your terminal is inside the
- Stuck?
- Leave a comment on the tutorial page or check the official Django docs:
https://docs.djangoproject.com/en/stable/intro/tutorial01/
- Leave a comment on the tutorial page or check the official Django docs:
What’s Coming in Part 3
Great work! You now have Python, a virtual environment, and Django installed. You’ve created your first project and seen the “Welcome to Django” page in your browser. In Part 3, we’ll:
- Create our first “app” inside the project (the blog app).
- Understand Django’s project vs. app structure.
- Set up a simple homepage so we’re ready to add content.
Get ready to dive into code and see your blog take shape! 🚀
Symons Ombori
SifaBrand.com – Making web development approachable
| GitHub: symonsombori