Photo by Brecht Corbeel on Unsplash
Creating your first Django application
Steps to create your first functioning Django App
Table of contents
- Download Python
- Create a Project Directory
- Create a Virtual Environment
- Install Django and Start a new project.
- You can run the development server.
- Create a new Django application e.g. blog
- Configure the new blog app in your mysite/settings.py file
- Let's create a "Hello world" display with our new blog app.
- blog/views.py controls the functions.
- mysite/urls.py controls the routing for web users
- blog/urls.py
- Run the migration to create the necessary tables.
- Start the development server once more.
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 Versions | Python Versions |
2.2 | 3.5,3.6,3.7 |
3.0 | 3.6,3.7,3.8 |
3.1 | 3.6,3.7,3.8,3.9 |
3.2 | 3.6,3.7,3.8,3.9,3.10 |
4.0 | 3.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?
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>
Feature | Virtual Environment Manager | How to Use | Key Differences |
Pipenv | Pipenv | pipenv install | Manages both dependencies and environment. |
pipenv shell | Automatically creates a virtual environment. | ||
pipenv install package_name | Combines Pipfile and Pipfile.lock for locks. | ||
pipenv lock | Native support for both PyPI and venv. | ||
Virtualenv | Virtualenv | virtualenv venv_name | Basic virtual environment creation. |
source venv_name/bin/activate | Manual activation of environment. | ||
pip install package_name | Creates isolated environments. | ||
Does not manage dependencies directly. | |||
Conda | Conda | conda create -n env_name | Supports multiple programming languages. |
conda activate env_name | Easily manages complex data science stacks. | ||
conda install package_name | Focuses 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
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