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
After activation, you'll see this:
(venv) C:\Users\YourName\my_django_project>
**
2.π¦ Install Django
**
pip install django
next lets run the code
python manage.py runserver
click the links provided and you with see such an image
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
**
4. Register Apps in config/settings.py
**
Open my_project/settings.py
and add the apps
INSTALLED_APPS = [
...
'users',
'attendance',
]
**
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
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')
blog/views.py
from django.shortcuts import render
def check_in(request):
return render(request, 'blog/check_in.html')
**
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'),
]
blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='users-home'),
]
**
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')),
]
**
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
**
π‘ 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)