edoburu / django-template-analyzer Goto Github PK
View Code? Open in Web Editor NEWExtract template nodes from a Django template
License: BSD 3-Clause "New" or "Revised" License
Extract template nodes from a Django template
License: BSD 3-Clause "New" or "Revised" License
Surprisingly calling get_node_instances(t.template, IfNode)
and get_node_instances(t.template.nodelist, IfNode)
are not equivalent. The latter is more complete and behaves to expectation. The former does not in that it misses all top level nodes.
It appears that treating t.template
as an iterator is not guaranteed to enumerate all nodes at the top level because django apparently expects all nodes to themselves be iterable, and return itself if it has no children.
Hello!
Thanks for writing and sharing this library, it's pretty handy! ๐ ๐ฎ
I've bumped into an issue when using a dynamic include like {% include variable_pointing_to_a_template_path %}
-- which is supported by Django, but fails with TemplateDoesNotExist
when you run get_node_instances(...)
on a template that contains that (see example project attached).
I believe it would be a better behavior to skip loading the template in these cases (when it's a dynamic variable instead of a hardcoded path).
This is the Traceback:
>>> from django.template.loader import get_template
>>> from django.template.base import VariableNode
>>> from template_analyzer.djangoanalyzer import get_node_instances
>>> tmpl = get_template('index.html')
>>> get_node_instances(tmpl, VariableNode)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 243, in get_node_instances
return _scan_nodes(nodelist, context, instances)
File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 158, in _scan_nodes
results += _extend_nodelist(node, context, instance_types)
File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 114, in _extend_nodelist
results += _scan_nodes(block.nodelist, context, instance_types, block, ignore_blocks=all_block_names)
File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 182, in _scan_nodes
results += _scan_nodes(subnodelist, context, instance_types, current_block)
File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/template_analyzer/djangoanalyzer.py", line 147, in _scan_nodes
template = get_template(node.template.var)
File "/home/elias/.virtualenvs/web/local/lib/python2.7/site-packages/django/template/loader.py", line 25, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
TemplateDoesNotExist: template_point
Here is the content of the main templates showing the issue (attached a full project reproducing it):
$ cat myapp/templates/index.html
{% extends "base.html" %}
{% block body %}
<h1>block index</h1>
{% with template_point="t1.html" %}
{% include template_point %}
{% endwith %}
{% endblock %}
$ cat myapp/templates/other.html
{% extends "base.html" %}
{% block body %}
<h1>block other</h1>
{% with template_point="t2.html" %}
{% include template_point %}
{% endwith %}
{% endblock %}
I'm getting this error (in combination with django-fluent-pages & contents) while I try to achieve to create a page type with a predefined template:
AttributeError at /admin/fluent_pages/page/1/
'ExtendsNode' object has no attribute 'parent_name_expr'
# admin.py
class HomePageAdmin(PlaceholderEditorAdmin):
def get_placeholder_data(self, request, obj):
# Tell the base class which tabs to create
template = self.get_page_template(obj)
return get_template_placeholder_data(template)
def get_page_template(self, obj):
return get_template('homepage/base.html')
# homepage/base.html
{% extends "base.html" %}
{% load placeholder_tags %}
{% block content %}
<h1>{{ page.title }}</h1>
{% page_placeholder "homepage" title="Main content" %}
{% endblock %}
Exception is in:
/template_analyzer/djangoanalyzer.py in _extend_nodelist, line 52
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.