Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling python "DIR" function from django template, or php print_r equivalent?

Does anyone know of an existing library for inspecting elements in the template?

Currently I usually end up doing this in the view:

show_me = dir(the_deets_on_this_object)
raise ValueError()

Then I can inspect the value of show_me in the debugger stack trace.

However, this is really ugly. I'd love to be able to do this inspection in the template, like is done in php with the print_r

{% load development_show_me %}
{{the_deets_on_this_object|show_them_to_me}}

I know that this doesn't exist in the django standard template library, but I'm hoping someone has already written this template filter so I can just happily use it.

Bonus behaviors:

  • Checks for settings.DEBUG and raises an error that isn't trapped if it is false
  • wraps the output in < pre >

I'll write it if I have to, but hopefully someone else has already done this.

like image 280
Ted Avatar asked Jan 15 '12 18:01

Ted


3 Answers

It ended up being faster to write the feature than to write the question asking if anyone knew if it existed.

#myproject/tempates/templatetags/helper_tags.py
from django.template import Library
from django.conf import settings
register = Library()

@register.simple_tag
def template_dir(this_object, its_name=""):
    if settings.DEBUG:
        output = dir(this_object)
        return "<pre>" + str(its_name) + " " + str(output) + "</pre>"
    return ""

Usage:

{% load helper_tags %}
{% template_dir field.field "field.field" %}

{% template_dir my_object "I'm looking at my_object" %}
like image 86
Ted Avatar answered Nov 10 '22 05:11

Ted


In django there is much better than print_r:

  • if the template crashes, you have access to all variable in the debig template. you can force this by settings {% url fjkdslmjfdklmfjlsmqk %} It's like a super print_r on all variables.

  • you can install django(extensions and werkzeug, this way you can replace runserver with runserver_plus and get the same page as the one we talked about, but with interactive Python shell embeded at every line of the stack trace

  • you can install django_template_repl, which will let you start the debugger at anyplace in the template, in the form of a python shell or a template shell.

  • you can install the django-debug-toolbar to get print_r style display and much more in a JS side bar invisible to anybody but you.

like image 32
e-satis Avatar answered Nov 10 '22 05:11

e-satis


I'm from a Drupal/PHP background and Debugger Tags helped me find something familiar to that environment.

"Allows you to use debugger breakpoints on Django templates."

after following the simple install, put this at the top of your template:

{% load debugger_tags %}

Then use this syntax for either the ipdb, pdb or wdb filters:

{% for object in object_list %}
    {{ object|pdb }}
{% endfor %}

Refresh the browser window your template is using. Then go into your terminal/shell where you are running the python/django server and explore the object:

-> return obj
(Pdb) dir(obj)

After looking at the valid attributes returning from dir() I can find and explore what I need to put into the template:

(Pdb) obj.post_category
<PostCategory: Meme>
(Pdb) obj.post_category.name
'Meme'

When exploring an object:

(Pdb) pp(obj.__dict__)

Hope this helps a noob like me :-J

like image 37
n8thanael Avatar answered Nov 10 '22 05:11

n8thanael