Skip to main content

Provides a model field and form field for allowing users to select a template file

Project description

Author:

Keryn Knight

Version:
0.1.1

Release

Status

stable (0.1.1)

travis_stable

master

travis_master

What it does

Provides a new model field, fields.TemplateField which allows for selection of a specific Django template using a nice form field (fields.TemplateChoiceField) and a nicer widget (widgets.TemplateSelector) than the standard <select> dropdown.

Example

Below is a screenshot of the admin widget, without any thumbnails set up for each template, because I’m too lazy for that.

Example

Use case

Imagine you have a Page model, and you want to allow admins or page authors to choose from a range of templates for the page to use, you could do this:

from django.dbimport models
from templateselector.fields import TemplateField

class MyPage(models.Model):
  title = models.CharField(max_length=100)
  # ...
  template = TemplateField(match='^myapp/mypage/layouts/.+\.html$')

which would allow them to select any HTML file Django could find in the appropriate directory.

Available functionality

These fields are really the only public API. There’s obviously other stuff, if you care to rummage around.

TemplateField

Extends CharField, and requires a match argument which ought to be a string version of a regular expression. The match will be used to filter the possible choices. Optionally also takes a display_name argument, which is a callable (or dotted.string.path.to.one) that takes a given string (the selected template path) and returns a nice name for it. The default form field for TemplateField is TemplateChoiceField

TemplateChoiceField

Has the same arguments as TemplateField, and can be used independently in all forms if you want to not use the model field. The form field, when rendered with the TemplateSelector widget, will try and show a preview image for each template, by attempting to load a 100x100 image from your staticfiles. Given a template name of path/to/template.htm it will try and load path/to/template.htm.png prefixed by whatever your STATIC_URL is. If no file exists, a placeholder image is shown as a fallback.

Both this and the TemplateField make use of…

nice_display_name

This function is the default callable for the display_name arguments on the TemplateField and TemplateChoiceField, it tries to provide some flexibility and sensisble defaults; specifically:

  • If your project defines a TEMPLATESELECTOR_DISPLAY_NAMES setting which is a dictionary like {'path/to/template.html': "my awesome template"} then the name "my awesome template" will be shown by preference.

  • If not set, or no key match is found, the function will take the file name (not the path!) without any extension and will attempt to make a pretty, readable name of it by replacing most non-alphabet characters with spaces, so the template test/app/hello_world.html would become Hello world

Supported Django versions

The tests are run against Django 1.11 on Python 2.7, and 3.5. The widget uses the Django 1.11 template-based-rendering, so won’t work on previous versions. Possibly it’ll just default back to a normal radiobox? I dunno.

Installation and usage

This is currently only available via git …

Installation

You can use pip to install the 0.1.1 version from PyPI:

pip install django-templateselector==0.1.1

Or you can grab it from GitHub like this:

pip install -e git+https://github.com/kezabelle/django-template-selector.git#egg=django-template-selector

Configuration

To get the TemplateSelector widget to display correctly, you will need to add templateselector to your project’s INSTALLED_APPS.

Running the tests

If you have a cloned copy, you can do:

python setup.py test

If you have tox, you can just do:

tox

Running the demo

A barebones demo is provided. It assumes you’re using something like virtualenv and virtualenvwrapper but you can probably figure it out otherwise:

mktmpenv --python=`which python3`
pip install -e git+https://github.com/kezabelle/django-template-selector.git#egg=django-templateselector

Then probably:

cd src/django-templateselector
python demo_project.py runserver

The index page / will show you a normal version of the selection widget, while /admin/tests/mymodel/add/ will show the slightly customised version for the standard Django admin.

The license

It’s the FreeBSD. There’s should be a LICENSE file in the root of the repository, and in any archives.


Copyright (c) 2017, Keryn Knight All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Change history for django-templateselector

0.1.1

  • First release on PyPI.

0.1.0

  • Initial development.

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-templateselector-0.1.1.tar.gz (27.7 kB view hashes)

Uploaded Source

Built Distribution

django_templateselector-0.1.1-py2.py3-none-any.whl (21.1 kB view hashes)

Uploaded Python 2 Python 3

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