Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of all reserved keywords in django templates?

I need a list of all the reserved keywords used by django's templating engine. Most of these keywords can be found here:

https://docs.djangoproject.com/en/dev/ref/templates/builtins/

Is there a programmatic way of getting just a list of the keywords? Or a document that contains all of them in list format?

like image 501
zzz Avatar asked Jan 08 '12 23:01

zzz


People also ask

How many reserved keywords are there in Python?

These keywords have to be used to develop programming instructions. Reserved words can't be used as identifiers for other programming elements like name of variable, function etc. Python 3 has 33 keywords while Python 2 has 30. The print has been removed from Python 2 as keyword and included as built-in function.

What does {{ this }} mean in Django?

{{ foo }} - this is a placeholder in the template, for the variable foo that is passed to the template from a view. {% %} - when text is surrounded by these delimiters, it means that there is some special function or code running, and the result of that will be placed here.

What does the Django templates contain?

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.


1 Answers

The django filters and tags are defined in the documentation at the link you provide - and that documented is created (using sphinx I think) automatically from the code defaultfilters.py code that Stefano suggests.

If it helps, then looking at the admindocs (admindocs) app will give you an even more accurate description as it will also include any custom tags and filters that you have defined.

from django.contrib.admindocs.views import load_all_installed_template_libraries
from django import template

app_libs = template.libraries.items()
builtin_libs = [(None, lib) for lib in template.builtins]

for module_name, library in builtin_libs + app_libs:
    for tag_name, tag_func in library.tags.items():
        print 'Tag: ', tag_name
    for filter_name, filter_func in library.filters.items():
        print 'Filter: ', filter_name

You have to run this from django-admin.py shell or python manage.py shell

like image 191
danodonovan Avatar answered Sep 19 '22 14:09

danodonovan