How to insert breakpoint in django template?

余生颓废 提交于 2021-02-19 08:43:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!