Effortless Django Installation: Utilizing Pip and Virtual Environments to Create a Sample Project on Ubuntu 22.04

Effortless Django Installation: Utilizing Pip and Virtual Environments to Create a Sample Project on Ubuntu 22.04

Simplifying Django Setup

Background: My Journey with Django and the AGTI STEM Bootcamp

Previously, when people approached me with their impressive Django-built websites, I perceived the framework to be complex. However, I soon discovered that my initial concerns were unfounded. My own journey with Django began a few weeks ago during my participation in the STEM Bootcamp organized by the AYDIA Gender and Technology Initiative (AGTI) at the America Center Kampala. This remarkable free program aims to empower and support women and girls in the fields of Science, Technology, Engineering, and Math (STEM), while also promoting technological innovations that address the current social needs in our ever-changing digital world, under the guidance of AYDIA Gender and Technology Initiative. During the coding sessions of the Bootcamp, we dived into web development using Django, and it became evident to me that Django is no different from any other framework. Today, I am thrilled to share how effortlessly and quickly Django can be installed, enabling you to utilize its power and create stunning projects in less than 1 hour. I wanted to share a collection of photos capturing memorable moments with the incredible ladies from STEM BootCamp Cohort 5. These pictures were taken during our engaging class sessions as well as during our well-deserved breaks.

Setting up Django with Pip in a Virtual Environment: A Comprehensive Guide

When developing Django projects, having a clean and isolated environment is crucial for smooth execution. In this step-by-step guide, we will walk you through the process of setting up Django with Pip in a virtual environment on your Ubuntu system. By following these steps, you will ensure that your Django projects are neatly organized, easily manageable, and free from any conflicts with other Python packages or dependencies.

Let's dive right in and get started!

Step 1: Updating Your System

Before we begin, let's ensure that your system is up to date. By running a few commands, we will update the package lists and upgrade any outdated packages, guaranteeing a stable foundation for our Django installation.

sudo apt update

Step 2: Verifying Python Installation

Next, we will check if Python is already installed on your computer. Python 3 comes pre-installed on Ubuntu 22.04 and other Debian Linux distributions. This step is crucial, as Django relies on Python as its programming language. By running this simple command, we can confirm if Python is present and which version is installed.

python -V

Step 3: Installing Pip

To manage Python packages effectively, we need to install Pip, the package installer for Python. With Pip, we can effortlessly install Django and any other required dependencies for our projects.

sudo apt install python3-pip python3-venv -y

Note: This command installs Pip for Python 3 and the Python 3 virtual environment package, automatically answering "yes" to any prompts or confirmations that may appear during the installation.

Step 4: Creating a Project Directory

Now that we have our system prepared, it's time to set up a virtual environment for your Django project. We'll create a new project directory and navigate to it, ensuring a dedicated space for our project's files and dependencies.

mkdir ~/djangoDev

cd ~/djangoDev

Step 5: Initializing the Virtual Environment

In this step, we'll create the virtual environment itself. By running this command, we will set up a clean and isolated environment where we can install Django and its associated packages without interfering with the global Python installation.

python3 -m venv my_env

Note: This command creates a virtual environment named "my_env" using the "venv" module in Python 3.

We can see the virtual environment by running the command ls

Step 6: Activating the Virtual Environment

Once our virtual environment is created, we need to activate it to start using it. We'll show you how to run a simple command that activates the environment, ensuring that any subsequent installations or executions take place within the isolated environment.

source my_env/bin/activate

  • Command Explanation:

  • "source": It is a shell built-in command used to read and execute commands from a file in the current shell session. In this case, it reads and executes the commands from the activation script of the virtual environment.

  • "my_env/bin/activate": It is the path to the activation script within the virtual environment. The activation script sets up the environment variables and modifies the shell prompt to indicate that the virtual environment is active.

Step 7: Installing Django with Pip

Now that our virtual environment is up and running, we can install Django seamlessly using Pip. The best part? Since we are working within our virtual environment, there's no need for administrative privileges (use of sudo), making the installation process straightforward and hassle-free.

pip install django

Step 8: Verifying Django Installation

To ensure that Django has been installed successfully, we'll run a quick command to verify the version of Django that is currently installed in our virtual environment. This step acts as a sanity check, confirming that Django is ready to power your projects.

django-admin --version

By following these steps, you'll have Django installed and ready to use in a clean, isolated virtual environment. Whether you're starting a new Django project or working on an existing one, this setup ensures that you can manage dependencies effortlessly and maintain project-specific configurations without any conflicts. Let's begin now by working on our sample website.

Building a Blog Website with Django: Step-by-Step Guide to Creating a Sample Project

Continuing from the previous section where we successfully installed Django in a clean and isolated virtual environment, we are now ready to dive into creating our first sample project. In this section, we will walk you through the steps of building a sample project using the newly installed Django framework.

Let's get started on creating our sample project!

Step 1: Opening the folder in Your Preferred Editor

First, we will open the djangoDev folder in your preferred code editor. For example, if you're using Visual Studio Code, you can easily open the project by executing a simple command:

code .

This command will launch the djangoDev folder in Visual Studio Code, allowing you to explore and modify the project's files and directories conveniently.

Step 2: Initializing the Project Structure

Now, it's time to lay the foundation of our project by creating its structure. We will achieve this by utilizing the django-admin command-line tool that ships with Django and provides various administrative functionalities with the startproject subcommand used to create a new Django project from a predefined project template. Since our project revolves around a website, we will name it 'Website' You can replace it with a name of your choice. To initiate the project creation process, execute the following command:

django-admin startproject Website

This command will generate the necessary files and directories for our Django project.

It's also important to ensure that you are still within the activated virtual environment when executing this command.

Step 3: Navigating to the Project Directory and Checking for Errors

Next, we will move into the newly created project directory, which is named "Website." Navigate to this directory using the following command:

cd Website

Once you're in the project directory, it's a good practice to verify if any errors exist in our project configuration. Run the following command to perform a preliminary check:

python manage.py check

This command will analyze the project structure and settings, ensuring everything is in order before proceeding further.

Step 4: Migrating the Database

Now that our project structure is set up, we need to migrate the database to create the necessary tables and schemas. Execute the following command:

python manage.py migrate

This command will apply any pending database migrations, ensuring that our project's database is up to date.

Step 5: Starting the Blog Website

Since our project focuses on blog posts, we will create a new app specifically for handling blog-related functionality. Use the following command to start the "blog" app:

python manage.py startapp blog

Executing this command will generate the necessary files and directories for the "blog" app within our Django project.

Remember to also check for any errors

Step 6: Start the Django development server

To start the Django development server, run the command python manage.py runserver This command will launch the server, The development server will run on http://127.0.0.1:8000/ by default.

python manage.py runserver

Step 7: Open the project in a web browser

Open a web browser (e.g., Chrome, Firefox) and enter the URL http://127.0.0.1:8000/ in the address bar. This will load your Django project, and you will be able to see it in the browser.

Congratulations! At this point, we have successfully created a sample project using Django. You can explore further customization and development of this Django project by following the Django Girls tutorial at https://tutorial.djangogirls.org/en/django/. You will be able to build a robust and feature-rich blog website. You can also Check out part two of this article right here Errors Encountered