π Getting Started with Django: A Two-App Project with Individual Templates
As a beginner learning Django, I wanted to go beyond the usual single-app setup and try something more practical: a Django project with two apps, where each app has its own templates.
This post walks you through exactly how I set it up β with actual code and screenshots from my own project. If youβre getting started or want to organize your project more cleanly, this guide is for you. π§ β¨
ποΈ Folder Structure
Hereβs what the structure looks like in my project:
web_project/
β
βββ hello/
β βββ templates/
β β βββ hello/
β β βββ sample.html
β β βββ sample.css
β βββ views.py
β βββ urls.py
β
βββ users/
β βββ templates/
β β βββ users/
β β βββ register.html
β βββ views.py
β βββ urls.py
β
βββ web_project/
β βββ settings.py
β βββ urls.py
βββ manage.py
β Step-by-Step Setup
βοΈ Step 0: Set Up a Virtual Environment
Before we even touch Django, let's set up a clean Python environment for the project.
-
Create a virtual environment (you can name it
venv
or anything):
python -m venv venv
- Activate it:
- On Windows:
venv\Scripts\activate
- On Linux/macOS:
source venv/bin/activate
- Install Django:
pip install django
Once Django is installed inside your virtual environment, youβre ready to create your Django project!
1. Create the Project and Apps
django-admin startproject web_project
cd web_project
python manage.py startapp hello
python manage.py startapp users
2. Register Apps in settings.py
INSTALLED_APPS = [
...
'hello',
'users',
]
3. Write Your Views
hello/views.py
from django.shortcuts import render
def hello(request):
return render(request, 'hello/sample.html')
πΈ Hereβs how my code looks in VS Code:
4. Create Templates
π hello/templates/hello/sample.html
<!DOCTYPE html>
<html>
<head>
<title>About django</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a sample Django web project.</p>
<p>Welcome to the Hello World page!</p>
</body>
</html>
πΈ Hereβs my actual file in action:
π users/templates/users/register.html
<h2>This is the user registration page</h2>
5. Set Up URL Routing
hello/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello, name='hello'),
]
users/urls.py
from django.urls import path
from . import views
def register(request):
return render(request, 'users/register.html')
urlpatterns = [
path('', register, name='register'),
]
web_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include('hello.urls')),
path('users/', include('users.urls')),
]
6. Run the Server
python manage.py runserver
Now open your browser and visit:
- http://127.0.0.1:8000/hello/ β Youβll see the "Hello, World!" template
- http://127.0.0.1:8000/users/ β Youβll see the user register page
π§ Key Takeaways
- Each app can and should have its own
templates/
folder for better organization. - Always remember to include each appβs URL configuration inside the main project
urls.py
. - Restart the dev server when you make changes to settings or URL configurations.
π¬ Final Thoughts
Setting this up helped me understand Djangoβs project structure and how templates work. It also taught me how to keep things clean when scaling to multiple apps.
Let me know in the comments if you're trying something similar or hit a wallβIβll be happy to help out!
Would you like this in Markdown format or as a PDF export ready for Dev.to upload?
By: Vall Achieng
As a beginner learning Django, I wanted to go beyond the usual single-app setup and try something more practical: a Django project with two apps, where each app has its own templates.
This post walks you through exactly how I set it up β with actual code and screenshots from my own project. If youβre getting started or want to organize your project more cleanly, this guide is for you. π§ β¨
ποΈ Folder Structure
Hereβs what the structure looks like in my project:
web_project/
β
βββ hello/
β βββ templates/
β β βββ hello/
β β βββ sample.html
β β βββ sample.css
β βββ views.py
β βββ urls.py
β
βββ users/
β βββ templates/
β β βββ users/
β β βββ register.html
β βββ views.py
β βββ urls.py
β
βββ web_project/
β βββ settings.py
β βββ urls.py
βββ manage.py
β Step-by-Step Setup
1. Create the Project and Apps
django-admin startproject web_project
cd web_project
python manage.py startapp hello
python manage.py startapp users
2. Register Apps in settings.py
INSTALLED_APPS = [
...
'hello',
'users',
]
3. Write Your Views
hello/views.py
from django.shortcuts import render
def hello(request):
return render(request, 'hello/sample.html')
πΈ Hereβs how my code looks in VS Code:
4. Create Templates
π hello/templates/hello/sample.html
<!DOCTYPE html>
<html>
<head>
<title>About django</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a sample Django web project.</p>
<p>Welcome to the Hello World page!</p>
</body>
</html>
πΈ Hereβs my actual file in action:
π users/templates/users/register.html
<h2>This is the user registration page</h2>
5. Set Up URL Routing
hello/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello, name='hello'),
]
users/urls.py
from django.urls import path
from . import views
def register(request):
return render(request, 'users/register.html')
urlpatterns = [
path('', register, name='register'),
]
web_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include('hello.urls')),
path('users/', include('users.urls')),
]
6. Run the Server
python manage.py runserver
Now open your browser and visit:
- http://127.0.0.1:8000/hello/ β Youβll see the "Hello, World!" template
- http://127.0.0.1:8000/users/ β Youβll see the user register page
π§ Key Takeaways
- Each app can and should have its own
templates/
folder for better organization. - Always remember to include each appβs URL configuration inside the main project
urls.py
. - Restart the dev server when you make changes to settings or URL configurations.
π¬ Final Thoughts
Setting this up helped me understand Djangoβs project structure and how templates work. It also taught me how to keep things clean when scaling to multiple apps.
Let me know in the comments if you're trying something similar or hit a wallβIβll be happy to help out!
Top comments (0)