Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse Django templates for template tags

Tags:

python

django

Situation

I'm writing a checker program that checks Django templates. For example I want to check if all Django templates that use url 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 {% load url from future %} in their templates.

For example if my program parses the following Django template, I want it to raise an exception.

{% extends 'base.html' %}
<td>
  <a href="{% url first second %}">
  </a>
</td>

But this template should get parsed without exception.

{% extends 'base.html' %}
{% load url from future %}
<td>
  <a href="{% url 'first' second %}">
  </a>
</td>

I'm not limited to this simple example. I have other parsings to do. For example I want to check how many load template tags are present in the template.

Question

How can I elegantly solve this parsing problem?

  • I don't want to use regular expressions.
  • 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.
  • I want to run the program separately from Django. So I don't want Django to run the program itself (with render_to_response). (This is important)

Code

Please show me some code that can solve the example I mentioned. I want to detect whether {% load url from future %} is in the code. Also I want to check every url template tag and check if the first argument is quoted.

Bonus:

  • 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)
like image 942
Yasser Souri Avatar asked Apr 09 '13 07:04

Yasser Souri


People also ask

How do template tags work Django?

Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.

How do I use custom template tags in Django?

Create a custom template tagUnder the application directory, create the templatetags package (it should contain the __init__.py file). For example, Django/DjangoApp/templatetags. In the templatetags package, create a . py file, for example my_custom_tags, and add some code to it to declare a custom tag.

How do I use template tags?

The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads. The content inside <template> can be rendered later with a JavaScript. You can use the <template> tag if you have some HTML code you want to use over and over again, but not until you ask for it.


1 Answers

You say...

I want to check if all Django templates that use url template tag, use it with quotes on first parameter so that it is Django 1.5 compatible.

...and...

I don't want to use regular expressions.

...because...

the result of that might become a huge spaghetti code

...but, frankly, writing a parser from scratch is likely to be even messier than using a regular expression. I don't see what's so messy about a regex as simple as something like...

"{% *url +[^']"

...and I doubt there's a non-regex-based solution that's as terse as that.

With regards to...

Also I want to check that they have included {% load url from future %} in their templates.

If your intention is to ensure Django 1.5 compatibility, this is pointless. According to the Django 1.5 release notes, the new-style url tag syntax is enabled by default, so the line {% load url from future %} won't have any effect.

And in versions prior to 1.5, it's much simpler just to put...

import django.template
django.template.add_to_builtins('django.templatetags.future')

...at the bottom of your settings.py and be done with it. :-)

like image 147
Aya Avatar answered Nov 02 '22 18:11

Aya