A thin wrapper around the Django templating system to render any string as a template. It provides an easy way to render any user inputted string as a regular django template.
Project description
django-string-renderer
A thin wrapper around the Django templating system to render any string as a template. It provides an easy way to render any user inputted string as a regular django template.
Requirements
This wrapper uses no models and requires no other settings than a django
engine in the TEMPLATES
setting where the app is used.
This package uses type annotations and mypy
to check those annotations.
This package is only tested against Python >= 3.6 and Django >= 2.2.
Installation
Binary Package from PyPi
In order to use this app in a Django project, the simplest way is to install it from PyPi:
pip install django-string-renderer
From the Source Code
In order to modify or test this app you may want to install it from the source code.
Clone the GitHub repository and then run:
pip install -r requirements.txt -r requirements-dev.txt
Usage
Start by adding stringrenderer
to your INSTALLED_APPS
setting like this:
INSTALLED_APPS = (
...,
'stringrenderer',
)
Then use the StringTemplateRenderer
class to build a Django template instance from a string et render the string with the context of your choice:
import stringrenderer
template_string = "Hello {{ recipient.first_name }} {{ recipient.last_name }}!"
renderer = StringTemplateRenderer(template_string)
rendered_content = renderer.render_template(context=dict(recipient=recipient_1), request=request)
rendered_content = renderer.render_template(context=dict(recipient=recipient_2), request=None)
When the first rendering request occurs on a given StringTemplateRenderer
instance, a Template
object is built from the string passed to __init__()
, and cached for the next rendering operations with other contexts.
You may check the template syntax of a string like this:
import stringrenderer
template_string = "Hello {{ recipient.first_name }} {{ recipient.last_name }}!"
is_valid, syntax_error = check_template_syntax(template_string)
Testing
Get the source code from GitHub, follow the installation instructions above, and run the following command:
python runtests.py
This will run the test suite with the locally installed version of Python and Django.
Projects Using this App
This app is used in the following projects:
- MyGym Web: a web platform for managing sports clubs. django-string-renderer is used to render the messages and emails addressed to the members.
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
Built Distribution
Hashes for django-string-renderer-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f67791856694926b13a803b92aa4b53f579dad94f2c2c5ee233d16d2d5228fe7 |
|
MD5 | f2e37cb0769b7bd90c9cfd26a4be3223 |
|
BLAKE2b-256 | 3978f5c39f4ff91d6565fd7272750d5b1f847ab1a6da39ec037920fc57659fbc |
Hashes for django_string_renderer-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd4047c1a781990191f29b6158d42473ad9255870d7e90bd125b98e3d0cc2d37 |
|
MD5 | 8ad4250ce62c7edc3a2d040393b3660d |
|
BLAKE2b-256 | cf748a7237c48758f5787c737d98072f1119a73ab4b715d724d9bfa7340ea86d |