DEV Community

lilo-creator
lilo-creator

Posted on

πŸš€ How I Created Two Django Applications with Their Own Templates

In todays Django learning i successfully built a project that contains two separate applications, each with its own HTML templates. In this article, I’ll walk you through what I did and what I learned β€” from setting up the project to organizing templates. Before you start the project you need to have;

  • 1.Python (version 3.10 or higher recommended)
  • 2. Code Editor ( Visual Studio Code)
  • 3.Browser

I followed the below steps:

**

1 πŸ›  Setting Up the Django Project

**

first open the vscode and open the terminal
then Create a Virtual Environment & Activate It

python

   # Create a folder for your project
    mkdir my_django_project
    cd my_django_project

    # Create virtual environment
    python -m venv venv

    # Activate the virtual environment
    .\venv\Scripts\activate  # for Windows
Enter fullscreen mode Exit fullscreen mode

After activation, you'll see this:

 (venv) C:\Users\YourName\my_django_project>
Enter fullscreen mode Exit fullscreen mode

**

2.πŸ“¦ Install Django

**

pip install django
Enter fullscreen mode Exit fullscreen mode

next lets run the code

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

click the links provided and you with see such an image

Image description
**

3.πŸ› Create Django Project & Apps

**
use the following commands to to create the main project and the two applications

# Create the project
django-admin startproject my_project .

# Create two apps
python manage.py startapp users
python manage.py startapp blog

Enter fullscreen mode Exit fullscreen mode

**

4. Register Apps in config/settings.py

**
Open my_project/settings.py and add the apps

INSTALLED_APPS = [
    ...
    'users',
    'attendance',
]

Enter fullscreen mode Exit fullscreen mode

**

5. Create Templates in Each App

**
In each app folder, create template directories like this:

users/
└── templates/
    └── users/
        └── home.html

blog/
└── templates/
    └── blog/
        └── check_in.html

Enter fullscreen mode Exit fullscreen mode

Then add some HTML like this in home.html:

**

6. Update views.py in each app

**

from django.shortcuts import render
users/views.py

def home(request):
    return render(request, 'users/home.html')

Enter fullscreen mode Exit fullscreen mode

blog/views.py

from django.shortcuts import render

def check_in(request):
    return render(request, 'blog/check_in.html')

Enter fullscreen mode Exit fullscreen mode

**

7. Create urls.py in both apps

**
users/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='users-home'),
]

Enter fullscreen mode Exit fullscreen mode

blog/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='users-home'),
]
Enter fullscreen mode Exit fullscreen mode

**

8.Update Main URLconf in config/urls.py

**

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('users.urls')),
    path('blog/', include('blog.urls')),
]

Enter fullscreen mode Exit fullscreen mode

**

9.Final Folder Structure Overview

**

my_django_project/
β”œβ”€β”€ my_project/
β”‚   └── settings.py, urls.py, ...
β”œβ”€β”€ users/
β”‚   β”œβ”€β”€ views.py
β”‚   └── templates/users/home.html
β”œβ”€β”€ blog/
β”‚   β”œβ”€β”€ views.py
β”‚   └── templates/blog/check_in.html
β”œβ”€β”€ venv/
β”œβ”€β”€ manage.py
Enter fullscreen mode Exit fullscreen mode

**

πŸ’‘ What I Learned

**
How Django structures projects and apps
How to route URLs within multiple apps
How to keep templates organized per app

Top comments (0)