How to insert breakpoint in django template?
Question:
How I can insert pdb.set_trace()
in django template? Or maybe run some another debug inside template.
Answers:
PyCharm Professional Edition supports graphical debugging of Django templates. More information about how to do that is here:
https://www.jetbrains.com/help/pycharm/2016.1/debugging-django-templates.html
PyCharm’s debugger is very, very good. It is just about the best Python IDE available.
Disclaimer: I am a satisfied customer, but have no other vested interest.
Here’s a little template tag I whipped up that allows you to inspect the context at any given point in a template:
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def set_breakpoint(context, *args):
"""
Set breakpoints in the template for easy examination of the context,
or any variables of your choice.
Usage:
{% load breakpoint %}
{% set_breakpoint %}
- or -
{% set_breakpoint your_variable your_other_variable %}
- The context is always accessible in the pdb console as a dict 'context'.
- Additional variables can be accessed as vars[i] in the pdb console.
- e.g. in the example above, your_variable will called vars[0] in the
console, your_other_variable is vars[1]
"""
vars = [arg for arg in locals()['args']] # noqa F841
breakpoint()
Save this code in [your_project]/[your_app]/templatetags/your_template_tag.py
. (The templatetags
folder should be in the same directory as the templates
folder for [your_app]
.)
Now, restart the server. (Don’t forget this part! The template tag will not load until you restart the server!)
You can now call the template tag by placing the following code in your template, where you want the debugger to run:
{% load your_template_tag %}
{% set_breakpoint %}
- or -
{% set_breakpoint your_variable %}
Voila! Django’s server will now display a pdb shell for you, where you can examine the entire context of your template, e.g. context['request'].path
. (You can see which variables are available by calling the locals()
builtin.)
Obviously, this is for development use only. Don’t run this in production, and don’t leave breakpoints hanging around in your code.
How I can insert pdb.set_trace()
in django template? Or maybe run some another debug inside template.
PyCharm Professional Edition supports graphical debugging of Django templates. More information about how to do that is here:
https://www.jetbrains.com/help/pycharm/2016.1/debugging-django-templates.html
PyCharm’s debugger is very, very good. It is just about the best Python IDE available.
Disclaimer: I am a satisfied customer, but have no other vested interest.
Here’s a little template tag I whipped up that allows you to inspect the context at any given point in a template:
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def set_breakpoint(context, *args):
"""
Set breakpoints in the template for easy examination of the context,
or any variables of your choice.
Usage:
{% load breakpoint %}
{% set_breakpoint %}
- or -
{% set_breakpoint your_variable your_other_variable %}
- The context is always accessible in the pdb console as a dict 'context'.
- Additional variables can be accessed as vars[i] in the pdb console.
- e.g. in the example above, your_variable will called vars[0] in the
console, your_other_variable is vars[1]
"""
vars = [arg for arg in locals()['args']] # noqa F841
breakpoint()
Save this code in [your_project]/[your_app]/templatetags/your_template_tag.py
. (The templatetags
folder should be in the same directory as the templates
folder for [your_app]
.)
Now, restart the server. (Don’t forget this part! The template tag will not load until you restart the server!)
You can now call the template tag by placing the following code in your template, where you want the debugger to run:
{% load your_template_tag %}
{% set_breakpoint %}
- or -
{% set_breakpoint your_variable %}
Voila! Django’s server will now display a pdb shell for you, where you can examine the entire context of your template, e.g. context['request'].path
. (You can see which variables are available by calling the locals()
builtin.)
Obviously, this is for development use only. Don’t run this in production, and don’t leave breakpoints hanging around in your code.