Forked from https://github.com/coremke/django-leaf to provide django 3+ support. Render django templates based on URL path.
-
Install the package from pypi:
pip install django3-leaf
-
Add "leaf" and "mptt" to your INSTALLED_APPS:
INSTALLED_APPS = ( ... 'leaf', 'mptt', )
-
Add leaf urls to the end of your urlpatterns:
url(r'^', include('leaf.urls')),
django-leaf can be used to render both "static" and database-backed templates.
If you want to render a template when a user goes to /example/url/
, create one of the following files:
- example/url.html
- example/url/index.html
- pages/example/url.html
- pages/example/url/index.html
After installing django-leaf, the admin interface will have a new section called Pages
where you'll be able to create your page hierarchy.
To define your own page model, you need to extend from leaf.models.Page
.
There are a few fields available for customization:
- identifier: A unique identifier for your model. This will be used to associate page nodes with your page implementation. If you don't provide an
identifier
, one will be provided for you. - template: The template to render.
- admin_page_inline: The admin class to use when rendering the template fields inline. This defaults to the default
admin.StackedInline
. - admin_inlines: A list of other inlines to add to the admin.
Here's an example for creating a page with translations provided by django-parler:
# admin.py
from parler.admin import TranslatableStackedInline
class AboutPageInline(TranslatableStackedInline):
pass
# models.py
from django.db import models
from leaf.models import Page
from parler.models import TranslatableModel, TranslatedFields
from .admin import AboutPageInline
class AboutPage(Page, TranslatableModel):
admin_page_inline = AboutPageInline
identifier = 'about-page'
template = "about.html"
translations = TranslatedFields(
headline=models.CharField(max_length=255),
copy=models.TextField(blank=True)
)
def __unicode__(self):
return self.headline
When rendering the template, all of the model fields will be available on the page
context variable:
{{ page.headline }}
{{ page.copy }}
The root page can be added to django-leaf by adding a new page with slug=home
and parent=None
. All pages added under that will be added without the home
part in the path.
- Better documentation.
- More configuration options.