Naming Template Files
There isnβt a hard rule enforced by Django for naming template files, but there are strong community conventions and best practices that Django developers follow for consistency, scalability, and maintainability.
β Standard Conventions for Django Template Naming
1. Use lowercase with underscores
- Similar to Python file naming (
snake_case
). - Example:
2. Include the app name (namespacing)
- To avoid collisions between apps, prefix templates with the appβs name (or keep them in an app-specific subdirectory).
- Example project structure:
This allows rendering with:
3. Follow Djangoβs view type convention
-
Common suffixes based on the view:
-
list
β for list views (product_list.html
) detail
β for detail views (product_detail.html
)form
β for forms (user_form.html
orsignup_form.html
)confirm_delete
β for delete confirmation views (product_confirm_delete.html
)
This matches Djangoβs generic class-based views naming.
4. Use base
and inheritance
- Standard to create a base layout:
5. Partial templates (reusable blocks)
- Prefix with
_
to indicate theyβre partials (not full pages).
And include them:
6. Keep consistency
- Use plural for list views:
products_list.html
- Singular for detail views:
product_detail.html
β Example structure:
templates/
base.html
shop/
product_list.html
product_detail.html
_product_card.html
accounts/
login.html
signup.html
π So the standard best practice:
snake_case
filenames- App-based directories for namespacing
- Use
list/detail/form/confirm_delete
suffixes aligned with Djangoβs generic views - Use
_partial.html
for includes
Perfect π Let me give you a standard, production-ready Django template folder structure that scales well for medium-to-large projects.
This follows Djangoβs conventions + clean architecture practices.
π Recommended Template Folder Structure
project_root/
β
βββ templates/ # Global templates folder
β βββ base/ # Base layouts
β β βββ base.html # Main site-wide layout
β β βββ base_auth.html # For auth pages (login/signup)
β β βββ base_admin.html # For admin/staff dashboards
β β
β βββ partials/ # Reusable partials
β β βββ _navbar.html
β β βββ _footer.html
β β βββ _pagination.html
β β βββ _messages.html # Flash messages
β β βββ _form_fields.html
β β
β βββ errors/ # Error pages
β β βββ 403.html
β β βββ 404.html
β β βββ 500.html
β β
β βββ shop/ # App-specific templates
β β βββ product_list.html
β β βββ product_detail.html
β β βββ product_form.html
β β βββ product_confirm_delete.html
β β βββ _product_card.html
β β
β βββ accounts/ # App-specific templates
β β βββ login.html
β β βββ signup.html
β β βββ profile.html
β β βββ password_change_form.html
β β βββ password_reset_form.html
β β
β βββ dashboard/ # Another app (e.g. staff dashboard)
β βββ dashboard_home.html
β βββ reports.html
β βββ stats.html
β
βββ static/ # Static files (CSS, JS, Images, etc.)
β βββ css/
β βββ js/
β βββ images/
β
βββ manage.py
β Best Practices in This Structure
-
Base Layouts
-
base.html
: global layout (navbar, footer, container). base_auth.html
: minimal layout for login/register pages.-
base_admin.html
: separate look for dashboards. -
Partials
-
Always prefix with
_
(like in Django and Flask conventions). -
Makes it clear they are included templates:
-
Error Pages
-
Custom
403.html
,404.html
,500.html
inerrors/
. -
Django automatically looks for these if configured.
-
App-Specific Namespacing
-
Each app has its own folder under
templates/
. -
This avoids collisions between apps (
shop/product_list.html
vsaccounts/user_list.html
). -
Generic View Compatibility
-
Use suffixes:
_list.html
,_detail.html
,_form.html
,_confirm_delete.html
. -
Matches Djangoβs class-based views and reduces mental overhead.
-
Consistency
-
Always use
snake_case
. - Plural for lists (
products_list.html
), singular for details (product_detail.html
).
β‘ Example: Rendering a shop product list