A Django package that automatically converts src and href attributes in HTML templates to Django's static template tags, making it easier to manage static files efficiently.
Project description
Description
This package modifies the HTML content of a file by replacing all <link> tags' href attributes, all <script> tags' src attributes, and all <img> tags' src attributes with Django template tags. It also adds {% load static %} at the beginning of the file to facilitate efficient static file management.
Installation
To use this package, simply install it with pip. The beautifulsoup4 library is included by default, but if you encounter any issues, you can install it separately:
pip install django-static-converter
Usage
# settings.py
INSTALLED_APPS = [
# Other apps
'django_static_converter',
# Other apps
]
# This will process all HTML files in the 'templates' directory and its subdirectories.
Example
Suppose you have an HTML file named index.html that looks like this:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<h1>Hello, World!</h1>
<img src="image.png">
</body>
</html>
Running
py manage.py convert-static
will modify the file to look like this:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<link href="{% static 'styles.css' %}" rel="stylesheet">
<script src="{% static 'script.js' %}"></script>
</head>
<body>
<h1>Hello, World!</h1>
<img src="{% static 'image.png' %}">
</body>
</html>
In this modified version, the <link>, <script>, and <img> tags' href and src attributes have been replaced with Django template tags, and {% load static %} has been added to the beginning of the file.
Parameters
directory(str): The path to the directory containing HTML files to process.
Returns
None: The function modifies the input files in place.
Raises
FileNotFoundError: If the input files cannot be found or accessed.
Authors
Feel free to adjust any sections or details to better fit your project's specifics!
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-static-converter-1.0.3.tar.gz.
File metadata
- Download URL: django-static-converter-1.0.3.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11c171e55b4c044fb2761d67f13bc371d2a1c18988e6030cd42d441e9c451664
|
|
| MD5 |
20c7a149cffbfbad178ab1b9ac4dfb40
|
|
| BLAKE2b-256 |
6735c3fe267e416f9795a36417af1114ea892c4a81555f78903eda368665a534
|
File details
Details for the file django_static_converter-1.0.3-py3-none-any.whl.
File metadata
- Download URL: django_static_converter-1.0.3-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9e92f67ac7a005bc431b30f4e58aa41b347397719e72f85f9d1c0d6f9dd709a
|
|
| MD5 |
70dbf8fa64dfdffd734efbc117f2622f
|
|
| BLAKE2b-256 |
ec9028676a01fc350efda1f40b9273b5156ab427b7af82ed7eea77fd7447cc2f
|