Skip to main content

Django Templater

Create Django templates from HTML files

Python Version

Description

Django Templater is a command line tool that will help you to convert your HTML templates to Django templates.

Installation

> pip install django-templater

How does it work?

Convert entire project

This working mode will convert all HTML files in the specified directory to Django templates and split them into master page and child pages

To create a new Django project, you need to specify the main block name. The main block name is the name of the block that will be used in the base HTML file. The main block name is required.

For example:

Base HTML file

<!-- ./templates/base.html -->
<html lang="en">
    <head>
        <title>My Website</title>
    </head>
    <body>
        <div class="header" id="main_block">
            <h1>My Website</h1>
            <p>Welcome to my website</p>
        </div>
    </body>
</html>

Home HTML file

<!-- ./templates/home.html -->
<html lang="en">
    <head>
        <title>My Website</title>
    </head>
    <body>
        <div class="header" id="main_block">
            <h1>User Home</h1>
            <p>Welcome to the user home page</p>
            <img src="assets/images/logo.png" alt="Logo">
        </div>
    </body>
</html>

On the above example, the main block name is main_block. So, when you create a new Django project, the main block name will be used as the name of the block in the base HTML file.

Convert assets URL

This working mode will convert all assets URL in the specified directory to Django static URL.

For example:

Home HTML file

<!-- ./templates/home.html -->
<html lang="en">
    <head>
        <title>My Website</title>
    </head>
    <body>
        <div class="header" id="main_block">
            <h1>User Home</h1>
            <p>Welcome to the user home page</p>
            <img src="assets/images/logo.png" alt="Logo">
        </div>
    </body>
</html>

On the above example, the assets URL is assets/images/logo.png. So, when you create a new Django project, the assets URL will be converted to Django static URL:

<html lang="en">
    <head>
        <title>My Website</title>
    </head>
    <body>
        <div class="header" id="main_block">
            <h1>User Home</h1>
            <p>Welcome to the user home page</p>
            <img src="{% static 'assets/images/logo.png' %}" alt="Logo">
        </div>
    </body>
</html>

Quick Start

Convert entire project

Let say you have an HTML templates in ./templates directory.

  • Django master page will be: ./templates/base.html
  • Django home page will be: ./templates/home.html, ./templates/users.html or ./templates/home/setting.html
  • HTML main block we want to use is: main_block

We want to create a Django project in ./myproject directory and we want to use ./templates directory as Django templates directory, and the output will be in ./myproject directory.

> dt -b main_block ./templates/base.html -o ./myproject ./templates/* 

This command will generate the following files:

<!-- ./output/base.html -->
<html lang="en">
    <head>
        <title>My Website</title>
    </head>
    <body>
        <div class="header" id="main_block">
            {% block main_block %}{% endblock %}
        </div>
    </body>
</html>
<!-- ./output/home.html -->
{% extends "base.html" %}
{% load static %}
{% block main_block %}
    <h1>User Home</h1>
    <p>Welcome to the user home page</p>
    <img src="{% static 'assets/images/logo.png' %}" alt="Logo">
{% endblock %}

Convert assets URL

Let say you have a HTML template called ./templates/home.html and you want to convert all assets URL in this file to Django static URL.

> dtt templates/home.html

Note: If you don't set the output directory, the result will be printed to the console.

Advanced Usage

Convert entire project

Static Files

By default this project will copy all static files to the Django static directory. The static directory we'll be static in your output directory. You can change the static directory by using -s or --static option.

> dt -b main_block -m ./templates/base.html -o ./myproject -s assets ./templates/* 

Downloading Remote Files

You can also download remote assets automatically. If your HTML files contains remote assets, this project will download the remote assets and copy them to the static directory.

> dt -b main_block -m ./templates/base.html -D -o ./myproject ./templates/*

Remote templates

You can also use remote templates. This project will download the remote templates and copy them to the templates directory.

> dt -b main_block -m https://example.com/base.html -o ./myproject ./templates/*

Convert assets URL

Static Files

By default this project will copy all static files to the Django static directory. The static directory we'll be static in your output directory. You can change the static directory by using -s or --static option.

> dtt -s assets templates/home.html

Changing output directory

You can change the output directory by using -o or --output option.

> dtt -o ./myproject templates/home.html

Note: If you don't set the output directory, assets output will be relative to it.

Downloading Remote Files

You can also download remote assets automatically. If your HTML files contains remote assets, this project will download the remote assets and copy them to the static directory.

> dtt -D templates/home.html

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for django-templater 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-templater 0.2.0
File Size Uploaded Provenance
django_templater-0.2.0.tar.gz 6.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-templater 0.2.0
File Interpreter ABI Platform
django_templater-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.8 kB

Release files / django_templater-0.2.0.tar.gz

Download URL django_templater-0.2.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b40184d01e960dc88490a6b40dbe3b3304a4b62dee2e0ea7b3c335e9e0c18983
BLAKE2b-256 checksum
How to use checksums
ac37485c03d71a4d316f053006db8ed0cd5f08e51ad33a0932440725b39fd4ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1016-azure

Release files / django_templater-0.2.0-py3-none-any.whl

Download URL django_templater-0.2.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e8f9b606dce43d0670c7c5488a70d8b5e6aa8d3dcb71e83729dbbb17c8b585e
BLAKE2b-256 checksum
How to use checksums
cb02f405683be09a9906031f5e575e4138d967ac2a7eba1c797004d5f87887ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1016-azure

Security for django-templater 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page