Django is a powerful Python web framework for building Full Stack website quickly and easily.
Moreover, it helps you write clean, maintainable code. In addition, it offers a simple admin panel to manage your site efficiently. In this blog post, I’ll guide you step by step so you can get started with Django and ultimately create your first project.
To begin with, you need to install Python. Make sure to download it from the official website. During installation, don’t forget to check the option to add Python to your system PATH. Otherwise, you may face issues running Python from the command line.
After that, verify your installation by running python --version
. If everything is set up correctly, the terminal will show the version number.
Now, let’s talk about Django apps. In simple terms, a Django app is a self-contained module within a Django project. Each app performs a specific function or delivers a particular feature. For example, you might have one app for user authentication and another for blog posts.
Finally, always use a virtual environment for your Django projects. This way, you can manage dependencies easily and avoid conflicts between different projects.
Prerequisites
Let’s begin with some requirements
Python 3.10 or later
During installation on Windows, check the box that says:
“Add Python to PATH”
This ensures you can run python
from the command line without extra setup.
Always install Python 3.10+ (latest stable version). Python 2 is deprecated.
A code editor like VS Code or PyCharm
Step 1: Create a Virtual Environment
For projects, always create a virtual environment:
#For windows
python -m venv "name of your environment"
#for macOS or linux
python3 -m venv "name of your environment"
Step 2: Activate the Environment
Note: Here, env is the name of the environment
#for windows
env\Scripts\activate
#for macOS or linux
source env/bin/activate
Step 3: Install Django
Open your terminal or command prompt and run
pip install django
#to check version
django-admin --version
Step 4: Create Your Django Project
Run the following command to create a new project
django-admin startproject myproject
Navigate to your project folder:
cd myproject
Step 5: Now it’s time to create the App
“To put it simply, a Django app is a self-contained module that handles a specific task or feature within a Django project.”
“Essentially, a Django app is a modular component of a Django project that delivers a distinct feature or functionality.”
It contains files like models.py
, views.py
, apps.py, admin.py
, tests.py, etc. Can be reused across multiple Django projects. After creating it, you must add it to INSTALLED_APPS
the list in settings.py
.
python manage.py startapp myapp
Step 6: Add the app to the installed apps List
Your installed app list looks like this: you need to add it there
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
After that, create a urls.py
file in your app. It’s not required to create this file in every app, but it’s a good practice. Keeping separate urls.py
Files for each app make your project more organized and easier to manage.
The project URLs file looks like this:
#myproject urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
Step 7: Run the Development Server
Use this command to start the server
python manage.py runserver
Open your browser and go to
http://127.0.0.1:8000/
Step 8: Make Migrations
Before running migrations, make sure to check your database settings in the project’s settings.py
file. By default, Django uses SQLite, which is already set up for you and good for small projects.
For PostgreSQL:
#for windows
pip install psycopg2
# for linux
pip install psycopg2-binary
configuration structure of the database for PostgreSQL
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'your_database_name',
'USER': 'your_postgres_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
}
}
For MySQL:
#install mysqlclient
pip install mysqlclient
MySQL configuration structure.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database_name',
'USER': 'your_mysql_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
Migrations in Django are used to create and apply changes to your database schema (like tables and columns) based on your models.py
file.
python manage.py makemigrations
python manage.py migrate
Step 9: Create Super User
This is the final step to manage CRUD operations using Django’s admin panel. You need to create a superuser. Use the command below to create it:
python manage.py createsuperuser
Comment below if you doubt this section.