English 中文(简体)
• 将“DIR”功能从“django”模板或“php”印刷机等同功能称作“DIR”功能?
原标题:Calling python "DIR" function from django template, or php print_r equivalent?

是否有任何人知道一个现有的图书馆来检查模板中的内容?

目前,我最后认为:

show_me = dir(the_deets_on_this_object)
raise ValueError()

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

然而,这确实是令人怀疑的。 我很爱能够在模版中进行这一检查,例如与印刷机在实验室进行的检查。

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

我知道,这在django标准模板图书馆中确实存在,但我希望有人已经写过这一模板过滤器,因此我可以乐意使用该模板。

行为:

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

如果我需要的话,我就要写这封信,但希望其他人已经这样做。

最佳回答

在django,比 印本_r好得多:

问题回答

最后,它比写问题要更快,询问是否有任何人知道存在。

#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 ""

使用:

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

{% template_dir my_object "I m looking at my_object" %}




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签