问题
How I can insert pdb.set_trace()
in django template? Or maybe run some another debug inside template.
回答1:
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.
回答2:
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):
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 %}
Voila! Django's server will now display a pdb shell for you, where you can examine the entire context of your template.
Obviously, this is for development use only. Don't run this in production, and don't leave breakpoints hanging around in your code.
来源:https://stackoverflow.com/questions/38512458/how-to-insert-breakpoint-in-django-template