How I Built a Django Project with Two Apps and Templates Using Git Bash & VS Code
Hey,
In this post, I’ll explain how I created a Django project named django_project
using Git Bash and VS Code, and added two apps with views and templates for:
- Home
- About
- Contact
- Services
Everything is explained step-by-step to help beginners follow along easily.
Step 1: Open Git Bash and Create a Virtual Environment
To keep all the Python packages for this project separate, I created a virtual environment in Git Bash:
bash
python -m venv env
Then I activated it:
bash
source env/Scripts/activate
On Windows, this uses the Scripts folder inside env to activate.
Step2:Install Django
Once the virtual environment was activated, I installed Django:
bash
pip install django
This made the django-admin command available.
Step 3:Start a Django Project.
Next I created the project and named it django_project:
bash
This created a folder called django_project with important files like:
django-admin startproject django_project
cd django_project
manage.py
– for running commands
settings.py
– to configure your project
urls.py
– for connecting routes
wsgi.py
and asgi.py
– for deployment (can ignore for now)
Step 4:Create Two Apps
In Django, apps are like separate components or features. I created two:
bash
app1 will handle the Home and About pages
python manage.py startapp app1
python manage.py startapp app2
app2 will handle the Contact and Services pages
Step 5:Open the Project in VS Code.
Still inside Git Bash, I opened the project in VS Code with:
bash
This opened the full project directory in Visual Studio Code.
code .
Step 6:Register the Apps in settings.py .
In django_project/settings.py, I added both apps under INSTALLED_APPS so Django can recognize them:
python
Step 7:Configure the Main urls.py File.
INSTALLED_APPS = [
...
'app1',
'app2',
]
In django_project/urls.py, I imported include and connected both apps to the project URLs:
python
This makes sure that URLs from both apps are recognized.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app1.urls')),
path('app2/', include('app2.urls')),
]
Step 8:Create Views in Both Apps
In app1/views.py:
python
In app2/views.py:
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
def about(request):
return render(request, 'about.html')
python
from django.shortcuts import render
def contact(request):
return render(request, 'contact.html')
def services(request):
return render(request, 'services.html')
Each view will load a corresponding HTML page.
Step 9:Create urls.py in Both Apps.
I created a file named urls.py in each app.
app1/urls.py:
python
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
app2/urls.py:
python
from django.urls import path
from . import views
urlpatterns = [
path('contact/', views.contact, name='contact'),
path('services/', views.services, name='services'),
]
Step 10:Add HTML Templates.
At the root level (same level as manage.py), I created a folder called templates, and added four files:
arduino
templates/
├── home.html
├── about.html
├── contact.html
└── services.html
Each file contains basic HTML. Here's an example:
home.html:
Repeat similar HTML for about.html, contact.html, and services.html.
html
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to the Home Page</h1>
</body>
</html>
Step 11:Configure Template Path in settings.py
In settings.py, I told Django where to find the templates:
python
import os
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
},
]
Step 12:Run the Server and Test
I went back to Git Bash and ran:
bash
Then I opened the following URLs in my browser:
python manage.py runserver
`
http://127.0.0.1:8000/ → Home Page
http://127.0.0.1:8000/about/ → About Page
http://127.0.0.1:8000/app2/contact/ → Contact Page
http://127.0.0.1:8000/app2/services/ → Services Page
`
All templates displayed successfully!
Conclusion.
That’s how I built a Django project using:
- Git Bash for environment setup and command line
- VS Code for writing and exploring code
- Django apps for modular views
- Templates to render dynamic web pages
This setup is great for beginners, and you can now build on it with forms, authentication, databases, or styling with CSS.
Top comments (0)