Skip to main content

Convert your Angular built index.html file to Django Template syntax

Project description

ng2django

This is a simple Python tool that will convert an HTML file built by Angular2+ to Django Template Language. This way static files can be made available to an Agnular app that is served up with a Django project.

The script basically does two things:

  • Adds a {% load static %} tag to the top of the file
  • Changes the value of href and src attributes of link and script tags to {% static "<path-to-the-file>" %}

Dependencies

The script uses Beutifulsoup4. You can install it by running pip install -r requirements.txt.

Usage

Build your Angular app and place the files in <Django-project-root>/<app>/static/<optional-subdirectory>/.

The script takes two positional arguments:

  • source: The path to the HTML file you want to convert.
  • dest: Where you want the Django Template file saved.

Then you can run

$ python ng2django.py <Django-project-root>/<app>/static/index.html <Django-project-root>/<app>/templates/index.html

if you are storing your files directly in your Django app's static and templates directories (not recommended), or

$ python ng2django.py <Django-project-root>/<app>/static/<optional-subdirectory>/index.html <Django-project-root>/<app>/templates/<optional-subdirectory>/index.html -s <optional-subdirectory>

if you are using some subdirectory of those paths.

Optional Arguments

Short Long Description
-s <subdir> --subdir <subdir> Include the subpath under <Django-app>/static/ where your static files are stored.
-n --nodelete Do not delete the source file after converting.
-p --pretty Create a dest file that is more human readable.

Recommended Setup and Usage

Copy the ng2django.py file into your Angular project root.

Open you package.json file. Under scripts, change build to

ng build --prod --output-path <Your-django-project-root>/<Django-App>/static/ng.

Add a postbuild key under script and set the value to

python ng2django.py <Your-django-project-root>/<Django-App>/static/ng/index.html <Your-django-project-root>/<Django-App>/templates/ng/index.html -s ng.

Run npm run build and your Angular JS and CSS files will be saved to your Django app's static directory. Then, this script will run and convert your HTML file and save it in your Django app's templates directory.

Contributing

Contributions are welcome - submit an issue/pull request.

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

ng2django-0.1.3.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

ng2django-0.1.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file ng2django-0.1.3.tar.gz.

File metadata

  • Download URL: ng2django-0.1.3.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for ng2django-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c42e8ce3c319fed79886652546cd9dc5a1d0105f44773c143497512ff0a05d20
MD5 671bcdf08ac1a73395e520d36f9cef3e
BLAKE2b-256 ed99a4c42853e090edc5c97e3254d5c18b6b8828cfaa249fac6ae816bcf34e7c

See more details on using hashes here.

File details

Details for the file ng2django-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ng2django-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for ng2django-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8a8775d2445427b874edc3656d4ec99c755f15eded07560525bcb04637fa7bba
MD5 957bccca23bf2203fec4ce5dd2180b89
BLAKE2b-256 90110cac7b7c77d6d906169ac9bd1d4f04b39decae451047c38bef6bd3ad1d38

See more details on using hashes here.

Supported by

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