Today has been crazyyyy anyways I created a Django project with two applications each having their own templates and all.
What I was to do:
- Use Django’s project and app architecture
- Link two apps: trousers and tops
- Display templates for each app with an awesome look cause you know ;)
Step 1: Setting Up the Virtual Environment
Installed virtualenv (if not already installed):
sudo apt install python3-venv
Created a virtual environment inside the project folder:
python3 -m venv venv
This created a venv/ folder containing an isolated Python environment with its own pip, python, and site-packages.
Activated the virtual environment:
source venv/bin/activate
Your terminal prompt changes to indicate the virtual environment is active (e.g., (venv)). To deactivate, run:
deactivate
Installed Django inside the virtual environment:
python -m pip install django
Step 2: Starting the Django Project
Created a new Django project named boutique:
django-admin startproject boutique
cd boutique
The folder structure now looks like this:
boutique/
manage.py
boutique/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Step 3: Creating Django Apps
Created two apps named trousers and tops:
python manage.py startapp trousers
python manage.py startapp tops
Step 4: Creating Models That Interact
In trousers/models.py, defined a Trouser model:
from django.db import models
class Trouser(models.Model):
name = models.CharField(max_length=100)
brand = models.CharField(max_length=100)
def __str__(self):
return f"{self.name} by {self.brand}"
In tops/models.py, imported Trouser and defined a Top model that links to Trouser via a foreign key (for example, if a top is styled to match a trouser):
from django.db import models
from trousers.models import Trouser
class Top(models.Model):
name = models.CharField(max_length=100)
color = models.CharField(max_length=50)
matching_trouser = models.ForeignKey(Trouser, on_delete=models.CASCADE)
def __str__(self):
return f"{self.name} in {self.color}"
Ran migrations to apply these changes:
python manage.py makemigrations
python manage.py migrate
[Step 5](url): Creating Simple Views
In trousers/views.py, created a view to list all trousers:
from django.shortcuts import render
from .models import Trouser
def trouser_list(request):
trousers = Trouser.objects.all()
return render(request, 'trousers/trousers.html', {'trousers': trousers})
In tops/views.py, created a view to list all tops:
from django.shortcuts import render
from .models import Top
def top_list(request):
tops = Top.objects.all()
return render(request, 'tops/tops.html', {'tops': tops})
Step 6: Linking Templates with Style
Each app has its own templates directory (trousers/templates/trousers/ and tops/templates/tops/).
Used Bootstrap and a light CSS gradient background to make the pages look clean and polished.
Example: trousers/templates/trousers/trousers.html
👖 Trousers Collection
<li>{{ trouser.name }} by {{ trouser.brand }}</li>
</ul>
<a href="/tops/">View Tops</a>
Example: tops/templates/tops/tops.html
👚 Tops Collection
</ul>
<a href="/trousers/">View Trousers</a>
Step 7: Setting Up URLs to Connect Everything
Created a urls.py in each app folder.
trousers/urls.py:
from django.urls import path
from .views import trouser_list
urlpatterns = [
path('', trouser_list, name='trouser-list'),
]
tops/urls.py:
from django.urls import path
from .views import top_list
urlpatterns = [
path('', top_list, name='top-list'),
]
Included these app URLs in the main project’s boutique/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('trousers/', include('trousers.urls')),
path('tops/', include('tops.urls')),
]
Now you can visit:
/trousers/ to see the list of trousers
/tops/ to view the tops and their matching trousers
It’s only the beginning, breath
Top comments (0)