Template filter in Django is great. We usually use it when processing variable that passed from views into templates. There are several built-in filters in Django that powerfull to process variable in templates. You can see them on https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#ref-templates-builtins-filters. Here are some basic template filter eg :
1 | {{ bon|upper }} |
If you already have debug background using Pdb / Ipdb, then we will implement it into templates. We want to debug variable in templates using Pdb / Ipdb. So, first we need to create “templatetags” folder.
1 2 3 4 | cd YOUR_APP mkdir templatetags && cd templatetags touch __init__.py touch view_debug.py |
Then we edit “view_debug.py” :
1 2 3 4 5 6 7 8 | from django import template register = template.Library() @register.filter(name=’ipdb’) def ipdb(element): import ipdb; ipdb.set_trace() return element |
Now, on our templates, for instance , “detail.html” :
1 2 3 4 5 6 7 8 | {% extends "base.html" %} {% block title %} My Apps {% endblock %} {% load view_debug %} {% block content %} {{ my-variable|ipdb }} {% endblock %} |
Run your development server, go to page “detail” or some page that load detail.html, back to your development server and it will show you like this :
1 2 3 4 5 6 7 | Django version 1.3.1, using settings ‘django_crawl.settings’ Development server is running at http://0.0.0.0:8000/ Quit the server with CONTROL-C. > /home/ubuntu/htdocs/django_app/posts/templatetags/view_debug.py(8)ipdb() 6 def ipdb(element): 7 import ipdb; ipdb.set_trace() —-> 8 return element |
You can debug your variable now 🙂