Snippet List
This tag is inspired by how ng-class works in AngularJS. https://docs.angularjs.org/api/ng/directive/ngClass
**example:**
`context = {
'context_var': True,
'context_var_2': 'a'
}`
`{% classes "class_name_1": context_var == True, "class_name_2": context_var_2 == "a", "class_name_3": False %}`
output: **class_name_1 class_name_2**
- tag
- dict
- ng-class
- classes
Will help you retrieve the value from a dictionary with a supplied key, or the human-readable value from a choices tuple. Works as follows:
To retrieve the value of a dict:
`{{ crime_rates_dict|getval:"Chicago" }}` <-- will return value of `crime_rates_dict["Chicago"]`
To retrieve the human-readable value from a choices tuple:
`{{ country.COUNTRIES|getval:"US" }}` <-- will return "United States" in `COUNTRIES = (("US", "United States"),)`
- template
- templatetag
- choices
- dict
- tuple
This is a model that implements (most of) the python dictionary interface. Meaning, you can work with this model exactly like a python dictionary, and it handles querying the database for it's values, saving/deleting the helper objects, etc.
I wrote this originally when I needed to store an arbitrary dictionary in the database, and decided to work it up into a near-complete implementation of a dictionary.
In order to make sure that the dictionary is the most optimized possible, I have a static method that can be used for retrieval. Feel free to ignore it if you don't care about optimizing database queries.
Here's an example:
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from binder.models import Dictionary
>>> d = Dictionary.getDict('Foobar')
>>> print d
{u'Foobar': u'omgbbq', u'HAHAHAH': u"who's afraid of a big, black, bat?"}
>>> d['pot'] = 'The kettle is black.'
>>> print d
{u'Foobar': u'omgbbq', u'pot': u'The kettle is black.', u'HAHAHAH': u"who's afraid of a big, black, bat?"}
>>> print d['pot']
The kettle is black.
>>> for k, v in d.iteritems():
... print k +":", v
...
Foobar: omgbbq
HAHAHAH: who's afraid of a big, black, bat?
pot: The kettle is black.
>>> print d.keys()
[u'Foobar', u'HAHAHAH', u'pot']
>>> print d.values()
[u'omgbbq', u"who's afraid of a big, black, bat?", u'The kettle is black.']
>>>
There's several more functions that I've implemented; check the code to see. (An interesting note: DictField saves immediately upon making a change, which is good to keep in mind in case that functionality isn't expected.)
Hope someone finds this useful. :)
--Chris
- model
- python
- dict
- dictionary
Based on http://www.djangosnippets.org/snippets/592/, a simplified recurse template tag that will explore dict and list objects. Useful and straightforward for JSON documents (for instance from couchdb :p).
Actual usage example:
{% recursedict mydictionary %}
<ul>
{% loop %}
<li>{% if key %}<b>{{ key }}</b>: {% endif %}{% value %}</li>
{% endloop %}
</ul>
{% endrecurse %}
The main tag syntax is "recursedict var" where var is your dictionary name.
The "key" property will contain the current key, or None if the current value comes from a list (in other words the list (a, b, c) is handled like a very hypothetical {None: a, None: b, None: c} dictionary.)
{% value %} will output the current value, or recurse if the value is a list, tuple or dict.
- template
- templatetag
- json
- dict
- resurse
When you need to include a specific javascript file/code snippet in your page, it's always better to do it at the bottom of your page to avoid to block the rendering too soon. This tag provide you a nice way to include and launch only what is needed:
Example in an included template that need to display google maps:
{% dict js_file google_api %}
<script src="http://www.google.com/jsapi?key={{ MAPS_API_KEY }}" type="text/javascript" charset="utf-8"></script>
<script src="{{MEDIA_URL}}js/map.display.js" type="text/javascript">...</script>
{% enddict %}
{% dict js_code link_map %}
$('.show-map').click(function() {
...
});
$('.hide-map').click(function() {
...
});
{% enddict %}
Finaly you just have to add this to the very bottom of your base.html file:
....
</body>
{% for k,v in js_file.items %}
{{v}}
{% endfor %}
<script type="text/javascript">
/* <![CDATA[ */
{% for k,v in js_code.items %}
{{v}}
{% endfor %}
/* ]]> */
</script>
</html>
- template
- javascript
- dict
- stack
10 snippets posted so far.