Easily integrate any HTML template into your Django project
Project description
Django Template Integrator
Overview
Django Template Integrator (DTI) is a Python CLI tool that automates the integration of frontend HTML templates into Django projects. It eliminates the manual, repetitive work of moving static assets, updating file paths, and converting standard HTML templates to Django's template format. The tool extracts ZIP files containing frontend templates, reorganizes the file structure to match Django's conventions (static/ and templates/ directories), and automatically rewrites asset paths to use Django's {% static %} template tags.
User Preferences
Preferred communication style: Simple, everyday language.
System Architecture
CLI Architecture
- Entry Point: Console script registered via setuptools (
django-template-integratorcommand) - Argument Parsing: Uses argparse for command-line argument handling
- Core Execution Flow: CLI layer delegates to the DjangoTemplateIntegrator core class
- Output Control: Verbose/quiet mode support for controlling log output
Rationale: Simple CLI architecture keeps the tool lightweight and easy to use. The separation between CLI parsing and core logic allows the integrator to be used programmatically if needed.
File Processing Pipeline
The tool follows a sequential pipeline:
- ZIP Extraction: Extracts frontend template ZIP to temporary directory with path traversal security checks
- Asset Detection: Identifies static assets (CSS, JS, images, fonts) by extension and path patterns
- File Relocation: Moves assets to Django's static/ directory structure, HTML files to templates/
- Path Rewriting: Parses HTML with BeautifulSoup and converts asset references to Django {% static %} tags
- Template Tag Injection: Adds {% load static %} to all HTML files
- Cleanup: Removes temporary extraction directory
Rationale: This pipeline approach ensures each step is independent and can be tested/modified separately. The temporary directory prevents conflicts with existing project files.
Asset Type Classification
Assets are categorized into subdirectories within static/:
- css/: Stylesheets (.css)
- js/: JavaScript files (.js)
- img/: Images (.jpg, .png, .gif, .svg, .ico, .webp, .bmp)
- fonts/: Font files (.woff, .woff2, .ttf, .eot, .otf)
- assets/: Fallback for unclassified files
Rationale: Django best practice organizes static files by type. This structure is conventional and makes asset management easier in production deployments.
HTML Processing Strategy
- Parser: BeautifulSoup4 for robust HTML parsing and manipulation
- Path Detection: Pattern matching for asset references in href, src, and other attributes
- Path Conversion: Replaces relative/absolute asset paths with Django template syntax
- Template Tag Insertion: Adds {% load static %} at the beginning of each HTML file
Rationale: BeautifulSoup handles malformed HTML gracefully and provides a reliable DOM manipulation API. This is safer than regex-based replacement for HTML modification.
Security Considerations
- Path Traversal Protection: Validates ZIP entries to reject absolute paths and directory traversal attempts
- Known Limitation: v1.0 does not explicitly block symlinks/hardlinks in ZIP files - designed for trusted template sources only
Rationale: Basic security measures prevent accidental or malicious file extraction outside the intended directory. The tool is designed for developer use with trusted frontend templates, not for processing untrusted user uploads.
Error Handling
- File not found errors for missing ZIP files
- ValueError exceptions for unsafe ZIP contents
- Path resolution validation during extraction
Rationale: Clear error messages help developers quickly identify configuration issues or problematic template files.
External Dependencies
Core Dependencies
- beautifulsoup4 (>=4.12.0)
- Purpose: HTML parsing and DOM manipulation
- Used for: Rewriting asset paths in HTML files and injecting Django template tags
- Why chosen: Industry-standard HTML parser with excellent handling of malformed markup
Python Standard Library
The tool relies on several Python built-in modules:
- zipfile: ZIP archive extraction
- pathlib: Modern path manipulation
- argparse: Command-line interface
- re: Regular expression pattern matching (for path detection)
- shutil: File operations (copying, directory removal)
Rationale: Minimal external dependencies keep the tool lightweight and reduce installation friction. Standard library modules provide all necessary functionality except HTML parsing.
Distribution
- setuptools: Package distribution and console script entry point registration
- pip: Installation method
Rationale: Standard Python packaging approach makes the tool easy to install and distribute through PyPI or direct installation.
Target Environment
- Django Projects: The tool outputs files structured for Django's static files and template system
- No Django Dependency: Intentionally does not require Django installation, allowing use in pre-Django setup phases or standalone environments
Rationale: Keeping Django as an implicit dependency (not explicit) allows developers to run the integration before Django is fully configured in their project.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_template_integrator-1.0.1.tar.gz.
File metadata
- Download URL: django_template_integrator-1.0.1.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5e40ae8d46c4eda2fe12c8e94280199fa819dee61b67007e7cc968caacd66c7
|
|
| MD5 |
7882dff79e8f297f36b9773b0388861f
|
|
| BLAKE2b-256 |
454c4ccba1244858cfff0e6257072dda6737aa1e574f4961ea87640e7a364a7d
|
File details
Details for the file django_template_integrator-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_template_integrator-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a983056ddc32c0c70954d424f8a8b0be515d5f6093d677d1d31c4b2f83ce9d2
|
|
| MD5 |
24a5b0adfe25d357e5b901cf53afbe96
|
|
| BLAKE2b-256 |
b678f65166504ad62057b815998efb4ca7d2b47979ce32765cf2beee3b92db30
|