Skip to main content

Organize your code and templates in a natural directory structure

Project description

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.

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

django_tree_view-4.2.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

django_tree_view-4.2.0-py3-none-any.whl (35.2 kB view details)

Uploaded Python 3

File details

Details for the file django_tree_view-4.2.0.tar.gz.

File metadata

  • Download URL: django_tree_view-4.2.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.11

File hashes

Hashes for django_tree_view-4.2.0.tar.gz
Algorithm Hash digest
SHA256 3ceb7d418f5c9f5e72ccf84a3286f96ce643c817253a578eb728d3f1c2eedeb9
MD5 83f8070fc04ed6b164182114236ad71d
BLAKE2b-256 20f3f3a157c214c26cc069d4f13b0d56cd9a04fb978c4bc429bc8fb7a82e8770

See more details on using hashes here.

File details

Details for the file django_tree_view-4.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tree_view-4.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b8167512a3ebf453c263a76e32014dc872b22fdd8aff98f35ad3169586ff3933
MD5 c230db672daa3d848213d0438735f75b
BLAKE2b-256 1f998dff4309ee8424882383a1b5ed9c22c90cd913e8454d25e563c68b325a5d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page