django_utils.templatetags package¶
Submodules¶
django_utils.templatetags.debug module¶
- class django_utils.templatetags.debug.Formatter(max_depth: int = 3)[source]¶
Bases:
_Formatter- MAX_LENGTH = 100¶
- MAX_LENGTH_DOTS = 3¶
- format(value: Any, depth: int | None, show_protected: bool, show_special: bool) → Any[source]¶
Call the formatter with the given value to format and optional depth
>>> formatter = Formatter() >>> class Eggs: ... pass >>> formatter(Eggs) '<Eggs {}>'
- format_datetime(value: date, depth: int, show_protected: bool, show_special: bool) → str[source]¶
Format a date
- Parameters:
value – a date to format
depth – the current depth
- Returns:
a formatted string
>>> formatter = Formatter() >>> formatter(datetime.date(2000, 1, 2)) '<date:2000-01-02>' >>> formatter(datetime.datetime(2000, 1, 2, 3, 4, 5, 6)) '<datetime:2000-01-02 03:04:05.000006>'
- format_dict(value: dict[Any, Any], depth: int, show_protected: bool, show_special: bool) → str[source]¶
Format a string
- Parameters:
value – a str value to format
depth – the current depth
- Returns:
a formatted string
>>> formatter = Formatter() >>> formatter({'a': 1, 'b': 2}, 5) '{a: 1, b: 2}'
- format_int(value: int, depth: int, show_protected: bool, show_special: bool) → Any[source]¶
Format an integer/long
- Parameters:
value – an int/long to format
depth – the current depth
- Returns:
a formatted string
>>> formatter = Formatter() >>> str(formatter(1, 0)) '1' >>> formatter(1, 1) '1'
- format_list(value: list[Any], depth: int, show_protected: bool, show_special: bool) → list[Any][source]¶
Format a string
- Parameters:
value – a list to format
depth – the current depth
- Returns:
a formatted string
>>> formatter = Formatter() >>> formatter(list(range(5))) '[0, 1, 2, 3, 4]'
- format_model(value: Model, depth: int, show_protected: bool, show_special: bool) → Any[source]¶
Format a string
- Parameters:
value – a str value to format
depth – the current depth
- Returns:
a formatted string
>>> formatter = Formatter() >>> from django.contrib.auth.models import User >>> user = User() >>> del user.date_joined >>> str(formatter(user, 5, show_protected=False)[:30]) '<User {email: , first_name: , '
- format_object(value: Any, depth: int, show_protected: bool, show_special: bool) → str[source]¶
Format an object
- Parameters:
value – an object to format
depth – the current depth
- Returns:
a formatted string
>>> formatter = Formatter() >>> original_max_length = formatter.MAX_LENGTH >>> formatter.MAX_LENGTH = 50
>>> class Spam(object): ... x = 1 ... _y = 2 ... __z = 3 ... __hidden_ = 4 >>> spam = Spam()
>>> str(formatter(spam, show_protected=False, show_special=False)) '<Spam {x: 1}>' >>> str(formatter(spam, show_protected=True, show_special=True)) '<Spam {x: 1, _Spam__hidden_: 4, _Spam__z: 3, __dict__:...}>'
>>> formatter.MAX_LENGTH = original_max_length
- format_str(value: bytes, depth: int, show_protected: bool, show_special: bool) → Any[source]¶
Format a string
- Parameters:
value – a str value to format
depth – the current depth
- Returns:
a formatted string
>>> formatter = Formatter() >>> str(formatter('test')) 'test' >>> str(formatter(b'test')) 'test'
- format_unicode(value: str, depth: int, show_protected: bool, show_special: bool) → str[source]¶
Format a string
- Parameters:
value – a unicode value to format
depth – the current depth
- Returns:
a formatted string
>>> formatter = Formatter() >>> original_max_length = formatter.MAX_LENGTH >>> formatter.MAX_LENGTH = 10 >>> str(formatter('x' * 11)) 'xxxxxxx...' >>> formatter.MAX_LENGTH = original_max_length
- django_utils.templatetags.debug.debug(value: Any, max_depth: int = 3) → SafeString[source]¶
Debug template filter to print variables in a pretty way
>>> str(debug(123).strip()) '<pre style="border: 1px solid #fcc; background-color: #ccc;">123</pre>'