Django models extension that allows render model from instance
$ pip install django-model-render
OR from source
python setup.py install
INSTALLED_APPS = (
...
'model_render',
...
)
If you use jinja or other custom template engine you can use MODEL_RENDER_DEFAULT_EXTENSION
to configute default template extension.
MODEL_RENDER_DEFAULT_EXTENSION = "jinja2"
default value is html
from django.db import models
...
from model_render import ModelRenderMixin
class MyModel(ModelRenderMixin, models.Model):
# ... fields here
Add model template in template folders and see how use it in templates:
{{ mymodelInstance.render }}
Model instance passsing in template context in variable named model
render()
method search template in <app_label>/models/<model_name>.<settings.MODEL_RENDER_DEFAULT_EXTENSION>
Something like
class MyModel(ModelRenderMixin, models.Model):
template_name="myapp/custom-template.html"
# ... fields here
And all calls render()
method will search in "myapp/custom-template.html"
.
Something like
{{ mymodelInstance.render("myapp/very-custom-template.html") }}
Only this call for render()
method will search in "myapp/very-custom-template.html"
.
render(template=..., additional={'myvar': 123})
adds myvar to template context.