oioioi.base.templatetags.simple_filters
¶
Module Contents¶
Functions¶
|
|
|
|
|
|
|
Lookup value from dictionary |
|
Lookup value from dictionary. Returns None if key |
|
Returns a value list corresponding to a key from Django's MultiValueDict |
|
Adds |
|
|
|
Adds css class to a django form field |
|
|
|
|
|
|
|
|
|
Be careful when using it directly in js! Code like that: |
|
This is a correct way of embedding json inside js in an HTML template. |
|
Escape string for generating LaTeX report. |
|
Attributes¶
- oioioi.base.templatetags.simple_filters.lookup(d, key)[source]¶
Lookup value from dictionary
Example:
{% load simple_filters %} {{ dict|lookup:key }}
- oioioi.base.templatetags.simple_filters.safe_lookup(d, key)[source]¶
Lookup value from dictionary. Returns None if key
key
is not present ind
.Example:
{% load simple_filters %} {{ dict|safe_lookup:key }}
- oioioi.base.templatetags.simple_filters.multival_lookup(d, key)[source]¶
Returns a value list corresponding to a key from Django’s MultiValueDict
- oioioi.base.templatetags.simple_filters.indent_string(value, num_spaces=4)[source]¶
Adds
num_spaces
spaces at the beginning of every line in value.
- oioioi.base.templatetags.simple_filters.add_class(field, css_class)[source]¶
Adds css class to a django form field :param field: form field :param css_class: css class :return: field with added class
Example usage
# my_app/forms.py ```python class MyForm(Form):
my_field = forms.CharField(max_length=100)
# my_app/views.py ```python def get_form(request):
my_form = MyForm() return render(request, ‘my_form.html’, { form: my_form })
# my_app/templates/my_form.html
`html {{ form.field|add_class:"my-class" }} `
would generate
`html <input class="my-class" id="my_field" name="my_field" /> `
- oioioi.base.templatetags.simple_filters.partition(thelist, n)[source]¶
From: http://djangosnippets.org/snippets/6/
Break a list into
n
pieces. Ifn
is not a divisor of the length of the list, then first pieces are one element longer then the last ones. That is:>>> l = range(10)
>>> partition(l, 2) [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
>>> partition(l, 3) [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> partition(l, 4) [[0, 1, 2], [3, 4, 5], [6, 7], [8, 9]]
>>> partition(l, 5) [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
You can use the filter in the following way:
{% load simple_filters %} {% for sublist in mylist|parition:”3” %}
- {% for item in sublist %}
do something with {{ item }}
{% endfor %}
{% endfor %}
- oioioi.base.templatetags.simple_filters.jsonify(value)[source]¶
Be careful when using it directly in js! Code like that:
- <script>
var x = {{ some_user_data|jsonify }};
</script>
contains an XSS vulnerability. That’s because browsers will interpret </script> tag inside js string.
- oioioi.base.templatetags.simple_filters.json_parse(value)[source]¶
This is a correct way of embedding json inside js in an HTML template.
- oioioi.base.templatetags.simple_filters.latex_escape(x)[source]¶
Escape string for generating LaTeX report.
Usage: {{ malicious|latex_escape }}
Remember: when generating LaTeX report, you should always check whether write18 is disabled! http://www.texdev.net/2009/10/06/what-does-write18-mean/