One solution is to make a script that renders all the templates based on an input dictionary of variables test values.
The main logic to retrieve the list of variables defined in the templates is the following:
from django.template.loader import get_template
def extract_required_vars(node):
if not hasattr(node, 'nodelist'):
return []
var_names = []
for child_node in node.nodelist:
if isinstance(child_node, VariableNode):
var_names.append(child_node.filter_expression.token)
elif isinstance(child_node, ForNode):
var_names.append(child_node.sequence.var.var)
elif isinstance(child_node, ExtendsNode):
template = get_template(child_node.parent_name.var)
var_names.extend(extract_required_vars(template))
elif isinstance(child_node, IncludeNode):
template = get_template(child_node.template.var)
var_names.extend(extract_required_vars(template))
var_names.extend(extract_required_vars(child_node))
return var_names
required_vars = extract_required_vars(get_template('index.html'))
The script then checks that the variables defined in the templates are either in the project settings or in the dict provided by user as test input.
/path/to/project/templates/templates/allusers.html
-> ok: users, STATIC_URL
/path/to/project/templates/entrer-en-contact.html
-> ok: contactform, STATIC_URL
/path/to/project/templates/dest-summary.html
-> ok: STATIC_URL
-> missing: dest_username
More details in this blog post.