Creating your first Django application

Steps to create your first functioning Django App

I will cover in this episode, the setting up of a virtual environment, installing Django, and creating a simple project with a simple Hello app in it.

Download Python

Being a Python web framework, Django requires you to download Python.

As a means of clarity, the Python versions compatible with some Django versions are outlined below as a means to guide your choice of a Python version.

Django VersionsPython Versions
2.23.5,3.6,3.7
3.03.6,3.7,3.8
3.13.6,3.7,3.8,3.9
3.23.6,3.7,3.8,3.9,3.10
4.03.8,3.9,3.10 and new releases

The latest version of Python of your choice can be found and downloaded at https://www.python.org/downloads/

What Python version could be most appropriate?
New versions of Python are often faster, are better supported and have more features, this is why the latest versions of Python3 are recommended by me. This will help you take advantage of the improvements and optimizations that come with the new Python releases.

On your cmd, you can verify Python is installed correctly by typing the command python or python3

Create a Project Directory

Create and select the location where you want to create your Django project and navigate to that directory in your terminal.

#Here , mysite is sserving as the project directory to house our DJango Projects 
cd C:/Users/HP/Desktop/mysite

Create a Virtual Environment

A virtual environment is a self-contained space where you can install and manage software separately from your main system. It helps keep projects isolated, preventing conflicts between different software versions. This is useful for developing and testing software without affecting your system's global settings.

The boldened statements are the essential reasons for a virtual environment.

In this tutorial, we would utilize the pipenv, to manage our virtual environment.

##Step 1 : First of all install the pipenv 
C:/Users/HP/Desktop/mysite > pip install pipenv 
##..................... Allow the command to run and complete its 
##Step 2: Activate the new virtual environment
C:/Users/HP/Desktop/mysite > pipenv shell  
#This command activates the virtual environment automatically
### Activated environment often displays as what you have below:
(...mysite...)C:/Users/HP/Desktop/mysite>
FeatureVirtual Environment ManagerHow to UseKey Differences
PipenvPipenvpipenv installManages both dependencies and environment.
pipenv shellAutomatically creates a virtual environment.
pipenv install package_nameCombines Pipfile and Pipfile.lock for locks.
pipenv lockNative support for both PyPI and venv.
VirtualenvVirtualenvvirtualenv venv_nameBasic virtual environment creation.
source venv_name/bin/activateManual activation of environment.
pip install package_nameCreates isolated environments.
Does not manage dependencies directly.
CondaCondaconda create -n env_nameSupports multiple programming languages.
conda activate env_nameEasily manages complex data science stacks.
conda install package_nameFocuses on scientific computing and libraries.
Manages environments and dependencies.

This table provides a brief comparison of three popular virtual environment managers: Pipenv, Virtualenv, and Conda, often used in Python/Django projects. Remember that the choice of a virtual environment manager depends on your project's requirements and your personal preferences, but as stated, earlier, I decided to utilize the Pipenv.

Install Django and Start a new project.

### Run the command to install django
(...mysite...)C:/Users/HP/Desktop/mysite> pipenv install django

#................................
#................................
### Start a project
(...mysite...)C:/Users/HP/Desktop/mysite>
django-admin startproject mysite .
###The essence of the "." is to prevent unnecessary repetition of the "mysite" since afterall we are in the directory already

You can run the development server.

 (...mysite...)C:/Users/HP/Desktop/mysite>
python manage.py runserver

This page after running the development server at localhost:8000 the first time shows you, the whole process was successful.

Create a new Django application e.g. blog

(...mysite...)C:/Users/HP/Desktop/mysite>
python manage.py startapp blog
#............................

Configure the new blog app in your mysite/settings.py file

#####settings.py
#............
INSTALLED_APPS = [
    # ...
    'blog',
    # ...
]

Let's create a "Hello world" display with our new blog app.

Below is the structure of our project and the application. The project folder is often distinguished by specific files such as

mysite/
├── manage.py
├── mysite/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── blog/
    ├── migrations/
    │   └── __init__.py
    ├── templates/
    │   └── blog/
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── models.py
    ├── tests.py
    └── views.py
    |__ urls.py  ###Simply create this manually in your apps

blog/views.py controls the functions.

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, world!")

mysite/urls.py controls the routing for web users

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')), 
    #"localhost:8000/blog" starts here and continues to the blog.urls 
     #to add other patterns to it 
    # eg. localhost:8000/blog/hello which goes to the hello view etc
]

blog/urls.py


from django.urls import path
from . import views
app_name='blog'

urlpatterns = [
    path('hello/', views.hello, name='hello'),
    #"localhost:8000/blog/hello" goes to the views.hello in the blog/views.py 
]

Run the migration to create the necessary tables.

(...mysite...)C:/Users/HP/Desktop/mysite>
python manage.py migrate
#......
#......

Start the development server once more.

(...mysite...)C:/Users/HP/Desktop/mysite>
python manage.py runserver
#incase you want the port on another asides 8000
# Run instead "python manage.py runserver:8001"

And there you are, on your browser, simply go to localhost:8000/blog/hello , and you should see a "Hello world" display.

Django is fun to learn, feel free to comment, and ask your questions, am also open to collaborations.

Follow me on Twitter here @Chasfat_Project

Follow me on Github https://github.com/Nobiscumdeus