Django Layers
Travis
django-layers makes it possible to serve a set of templates and static resources as defined in settings.py. This means you can serve different HTML, Javascript and CSS to eg. basic mobile devices, smart phones and desktop browsers. These template sets (aka layers) also stack, so if you create foo.html for basic devices it is automatically available for desktop browsers as well. You can override foo.html for desktop browsers.
Installation
Install or add django-layers to your Python path.
Add layers after django.contrib.static to your INSTALLED_APPS setting.
Ensure the app that you will be creating layers for appears first in INSTALLED_APPS else template override won’t work.
Example
Note: there is a working example in the example subdirectory.
We have sites example.com, basic.example.com and smart.example.com. Each of the sites have their own settings.py, thus different Django processes.
Directory structure
templates
- basic
- foo.html (1)
- bar.html (2)
- smart
- bar.html (3)
- web
- bar.html (4)
static
- basic
- foo.css (5)
- bar.css (6)
- smart
- bar.css (7)
- web
- bar.css (8)
Settings
We define an “inheritance” hierarchy.
Desktop settings has LAYERS = {'layers': ['basic', 'web']}.
Basic settings has LAYERS = {'layers': ['basic']}.
Smart settings has LAYERS = {'layers': ['basic', 'smart']}.
All settings require loaders and finders to be set. The order is important.
INSTALLED_APPS = (
'myapp',
'layers',
...
)
TEMPLATE_LOADERS = (
'layers.loaders.filesystem.Loader',
'django.template.loaders.filesystem.Loader',
'layers.loaders.app_directories.Loader',
'django.template.loaders.app_directories.Loader',
)
STATICFILES_FINDERS = (
'layers.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
'layers.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Template results
http://example.com/foo yields (1).
http://example.com/bar yields (4).
http://basic.example.com/foo yields (1).
http://basic.example.com/bar yields (2).
http://smart.example.com/foo yields (1).
http://smart.example.com/foo yields (3).
Static results
http://example.com/static/foo.css yields (5).
http://example.com/static/bar.css yields (8).
http://basic.example.com/static/foo.css yields (5).
http://basic.example.com/static/bar.css yields (6).
http://smart.example.com/static/foo.css yields (5).
http://smart.example.com/static/foo.css yields (7).
Overriding templates from other apps
The normal template resolution rules apply. Creating eg. templates/web/registration/login.html will override the login page for web only.
Collectstatic
Collectstatic remains unaffected. The collector delegates to finders, so all layer aware resources end up with partial paths under the STATIC_ROOT directory.
Decorators
A user could follow a link that leads him to a layer that serves a broken page. For example a web site is served on www.site.com with an accompanying basic site m.site.com. Visiting www.site.com/flashy-dashboard with a basic device like a Samsung E250 will result in the user being redirected to m.site.com/flashy-dashboard. That page probably does not exist for basic devices because it can’t render it well enough. In such a case a decorator exclude_from_layers is provided that renders a friendly page instead of a 404 or 500 error:
class WebOnlyView(TemplateView):
template_name = "layers/web_only_view.html"
@exclude_from_layers(layers=("basic",))
def get(self, *args, **kwargs):
return super(WebOnlyView, self).get(*args, **kwargs)
Changelog
0.2
Inevitable package rename.
0.1
Initial release.
Metadata
Release files for django-layers-hr 0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-layers-hr-0.5.tar.gz | 17.8 kB | Without provenance | Details for django-layers-hr-0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_layers_hr-0.5-py2.7.egg | Legacy Egg format | - | - | Details for django_layers_hr-0.5-py2.7.egg |
Total release size: 42.5 kB
Release files / django-layers-hr-0.5.tar.gz
| Download URL | django-layers-hr-0.5.tar.gz |
|---|---|
| Size | 17.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
753d43ba6a5afa8fc04a472658ed2c5acf806cb3d7495d875ccf483ff6a35b73
|
|
BLAKE2b-256 checksum How to use checksums |
f37aa741151443c7bda7c7854e8fcc36f18de3bb6df0cbdf3285013397248404
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_layers_hr-0.5-py2.7.egg
| Download URL | django_layers_hr-0.5-py2.7.egg |
|---|---|
| Size | 24.7 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
e5dde629884c179328ca94c0c6eba4392b1374330ea8201dd2765a8a0ad3e9f8
|
|
BLAKE2b-256 checksum How to use checksums |
f9db7f707faa1b455a5dbe26e757d77a6dbdf7c03630bcf6bee481ecba7456e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-layers-hr 0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.