Skip to main content

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-integrator command)
  • 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:

  1. ZIP Extraction: Extracts frontend template ZIP to temporary directory with path traversal security checks
  2. Asset Detection: Identifies static assets (CSS, JS, images, fonts) by extension and path patterns
  3. File Relocation: Moves assets to Django's static/ directory structure, HTML files to templates/
  4. Path Rewriting: Parses HTML with BeautifulSoup and converts asset references to Django {% static %} tags
  5. Template Tag Injection: Adds {% load static %} to all HTML files
  6. 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

  1. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_template_integrator-1.0.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_template_integrator-1.0.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file django_template_integrator-1.0.1.tar.gz.

File metadata

File hashes

Hashes for django_template_integrator-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a5e40ae8d46c4eda2fe12c8e94280199fa819dee61b67007e7cc968caacd66c7
MD5 7882dff79e8f297f36b9773b0388861f
BLAKE2b-256 454c4ccba1244858cfff0e6257072dda6737aa1e574f4961ea87640e7a364a7d

See more details on using hashes here.

File details

Details for the file django_template_integrator-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_template_integrator-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9a983056ddc32c0c70954d424f8a8b0be515d5f6093d677d1d31c4b2f83ce9d2
MD5 24a5b0adfe25d357e5b901cf53afbe96
BLAKE2b-256 b678f65166504ad62057b815998efb4ca7d2b47979ce32765cf2beee3b92db30

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page