Render and serve django templates based on URL.
Project description
django3-leaf
Forked from https://github.com/coremke/django-leaf to provide django 3+ support. Render django templates based on URL path.
Quick start
-
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')),
Usage
django-leaf can be used to render both "static" and database-backed templates.
Static 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
Database Backed Templates
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 }}
Home Page
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.
TODO
- Better documentation.
- More configuration options.
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
File details
Details for the file django3-leaf-3.0.1.tar.gz
.
File metadata
- Download URL: django3-leaf-3.0.1.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.4.0-113-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b4bb99c31b98220c76a4e69bdec72441e676d79e74e03de8297077e07c20241 |
|
MD5 | e96fcfc6e5c4073f963b46b8f4818b82 |
|
BLAKE2b-256 | b4338ec11c5f42495a100a4593eddbab00335e7b9d06aa0487d6bd0066155ded |
File details
Details for the file django3_leaf-3.0.1-py3-none-any.whl
.
File metadata
- Download URL: django3_leaf-3.0.1-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.4.0-113-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93b1187e5a69468f1c4cc2b2ea9fb3a405642633254b18c7fb28e5bb78a9aad6 |
|
MD5 | bdd0cf6f9191668bdae6a0e31d12a180 |
|
BLAKE2b-256 | d69c779e63989e300e5a790ece1de2e08f6e8ccc00787e3e853a028161662497 |