Snippet List
## How to use
Use this [admin filter](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter) together with a numeric field to allow filtering changlist by field
values range (in this case, age groups):
For example, to group customers by age groups:
class Customer(models.Model):
# ...
age = models.IntegerField()
age.list_lookup_range = (
(None, _('All')),
([0, 2], '0-2'),
([2, 4], '2-4'),
([4, 18], '4-18'),
([18, 65], '18-65'),
([65, None], '65+'),
))
class CustomerAdmin(admin.ModelAdmin):
list_filter = [('age', ValueRangeFilter), ]
## Inspiration
[This snippet](https://djangosnippets.org/snippets/587/) (for django < 1.4) inspired me to make this work for newer django versions.
**Designed to hold a list of pages and page ranges for a book/magazine index.**
A custom model field (and accompanying form field) that saves comma-separated pages and page ranges in human-readable string form. Includes some clean-up code, so that you can add a new page or range at the end of an existing entry, and it will put it in numeric order and combine runs into ranges. So this:
4-33, 43, 45, 60-65, 44, 59
becomes the tidy
4-33, 43-45, 59-65
**NOTE:** If you comment out the raising of the `ValidationError` in the form field's validate() method, it will actually clean up any extraneous characters for you (which could be dangerous, but for me is usually what I want), so even this horrible mess:
;4-33, 46a fads i44 ,p45o
gets cleaned to
4-33, 44-46
*This is the first custom field I've ever written for Django, so may be a little rough but seems to work fine.
- multiple
- stringformat
- range
- pages
- index
Accepts the same arguments as the 'range' builtin and creates
a list containing the result of 'range'.
Syntax:
{% mkrange [start,] stop[, step] as context_name %}
For example:
{% mkrange 5 10 2 as some_range %}
{% for i in some_range %}
{{ i }}: Something I want to repeat\n
{% endfor %}
Produces:
5: Something I want to repeat
7: Something I want to repeat
9: Something I want to repeat
This tag is meant to mimic the python use of range in a for-loop: 'for i in range(start, end, step)'. It is implemented like a loop and it takes both variable names from the context and constant integers as arguments.
Syntax:
{% range end as i %}
{{ i }}
{% endrange %}
{% range start:end as i %}
{{ i }}
{% endrange %}
{% range start:step:end as i %}
{{ i }}
{% endrange %}
Easy to use range filter.
Just in case you have to use a "clean" for loop in the template.
Inspired by [Template range tag](http://www.djangosnippets.org/snippets/779/)
Copy the file to your templatetags and load them.
[Django doc | Custom template tags and filters](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/)
These field and widget are util for to those fields where you can put a star and end values.
It supports most of field types and widgets (tested with IntegerField, CharField and DateField / TextInput and a customized DateInput).
**Example of use:**
class FormSearch(forms.Form):
q = forms.CharField(max_length=50, label=_('Search for'))
price_range = RangeField(forms.IntegerField, required=False)
**Example of use (with forced widget):**
class FormSearch(forms.Form):
q = forms.CharField(max_length=50, label=_('Search for'))
price_range = RangeField(forms.IntegerField, widget=MyWidget)
This is a simple tag that I am sure has been written before, but it helps people with the problem, 'how do I iterate through a number in the tempaltes?'.
Takes a number and iterates and returns a range (list) that can be
iterated through in templates
Syntax:
{% num_range 5 as some_range %}
{% for i in some_range %}
{{ i }}: Something I want to repeat\n
{% endfor %}
Produces:
0: Something I want to repeat
1: Something I want to repeat
2: Something I want to repeat
3: Something I want to repeat
4: Something I want to repeat
Template filter to format a start and end time in to a range. Uses Django's ["P" format](http://www.djangoproject.com/documentation/templates/#now) and assumes start and end time are on the same day or night before/morning after.
`{{ start_time|time_range:end_time }}`
Examples:
7-8 p.m.
8 p.m. - midnight
noon - 4 p.m.
9:45 a.m. - 5:15 p.m.
10:30 p.m. - 1:30 a.m.
9 snippets posted so far.