Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse Django templates for template tags
    text
    copied!<h2>Situation</h2> <p>I'm writing a checker program that checks Django templates. For example I want to check if all Django templates that use <code>url</code> template tag, use it with quotes on first parameter so that it is Django 1.5 compatible. Also I want to check that they have included <code>{% load url from future %}</code> in their templates.</p> <p>For example if my program parses the following Django template, I want it to raise an exception.</p> <pre><code>{% extends 'base.html' %} &lt;td&gt; &lt;a href="{% url first second %}"&gt; &lt;/a&gt; &lt;/td&gt; </code></pre> <p>But this template should get parsed without exception.</p> <pre><code>{% extends 'base.html' %} {% load url from future %} &lt;td&gt; &lt;a href="{% url 'first' second %}"&gt; &lt;/a&gt; &lt;/td&gt; </code></pre> <p>I'm not limited to this simple example. I have other parsings to do. For example I want to check how many <code>load</code> template tags are present in the template.</p> <h2>Question</h2> <p>How can I elegantly solve this parsing problem?</p> <ul> <li>I don't want to use regular expressions.</li> <li>I this Django it self has some utilities in this regard. I think using them is a good idea, but I don't know how.</li> <li>I want to run the program separately from Django. So I don't want Django to run the program itself (with <code>render_to_response</code>). (<strong>This is important</strong>)</li> </ul> <h2>Code</h2> <p>Please show me some code that can solve the example I mentioned. I want to detect whether <code>{% load url from future %}</code> is in the code. Also I want to check every <code>url</code> template tag and check if the first argument is quoted.</p> <p><strong>Bonus</strong>:</p> <ul> <li>I want to be able to see the rendered HTML that Django generates from this template, and do my HTML parsing on it. (for example with PyQuery)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload