Skip to main content

Django Tree View

Organize your view code and templates and in a directory structure, and automatically generate urls for them.

Terminology

"Tree View" refers the actual view function we've created, which processes a request according to code you write inside your "view tree".

Installation

  1. pip install django_tree_view

  2. In your urls.py file, add in a call to make_tree_view, passing in the python package name (dotted python path) of your view tree:

    from django_tree_view import make_tree_view
    
    urlpatterns = [
        make_tree_view('my_view_tree'),
    ]
    

    Note: we're not using django's path() here, and we didn't specify a url regex. make_tree_view() returns an object which dynamically resolves urls, matching them whenever there is a corresponding directory (with a view_tree_node.py module) inside of your view tree.

    Note: you can use include('some_path/', [make_tree_view()]) if you want a url prefix.

  3. Optionally, add the full path to your view tree to the DIRS option of your DjangoTemplates backend:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'my_view_tree')],
            ...
        },
    ]
    

    This will allow you to place page-specific templates inside the same directory which holds the view code for that url.

CSRF Protection

Our view uses Django's csrf_exempt decorator, and selectively applies its own CSRF protection.

Any view tree node can opt-out of CSRF protection by setting CSRF_EXEMPT=True at the module level.

The CSRF protection we use is not Django's default CSRF protection. We use utilities from django_referer_csrf. You may want to check out the documentation for django_referer_csrf, and use their middleware in place of Django's, but this is not required.

Organizing Your View Tree

TODO

TODO

When DEBUG and reloading, reload empty modules (not sure, but this might already be done).

"Single View Tree" approach

Our path resolver no longer matches when the "path directory" contains no view_tree_node.py file.

I think we should change how view trees are "installed". The user should define one global VIEW_TREE_ROOT. You can "install" reusable apps in your view tree by symlinking to them from your view tree.

This makes things easier to understand for the end user. They don't need understand make_tree_view(). You have a single directory containing your view tree. Period.

The challenge: if an app is distributed on pypi, symlinking to it (in a way that can be copied between server environments), is not trivial.

Release files for django-tree-view 4.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-tree-view 4.3.0
File Size Uploaded
django_tree_view-4.3.0.tar.gz 18.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-tree-view 4.3.0
File Interpreter ABI Platform
django_tree_view-4.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 53.2 kB

Release files / django_tree_view-4.3.0.tar.gz

Download URL django_tree_view-4.3.0.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b8072180bbdbb8d354eb06180381d55393f10bdd5d9965c154ddc3ce7545cc02
BLAKE2b-256 checksum
How to use checksums
bb516888598e8cbde6dff006347c8273be092f505e80cb2358c4cc1a4cf2bf09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.10.1 urllib3/1.26.7 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

Release files / django_tree_view-4.3.0-py3-none-any.whl

Download URL django_tree_view-4.3.0-py3-none-any.whl
Size 35.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b38116da11c9f644244a9dec045956c5740d82e7cf6f83023311f06a6d3325a9
BLAKE2b-256 checksum
How to use checksums
db6c7aaed65b7ec96bb828932518c133fbfa3afa054599c4612961c421fbdc1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.10.1 urllib3/1.26.7 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

Release history Release notifications | RSS feed

This release

4.3.0 This release

2 release files

4.2.0

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.1.3

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page