8

I often see something like that: something.property|escape


something is an object, property is it's string property. escape - i don't know :)
What does this mean? And what min python version it is used in?


EDIT: The question was asked wrongly, it said "What does | mean in Python", so the bitwise or answers are correct, but irrelevant, please do not downvote them

e100
  • 1,603
  • 2
  • 14
  • 21
  • It would be helpful if you could provide more context than just the expression. This is an operator that can be overloaded. – S.Lott Jan 06 '09 at 16:53

3 Answers3

14

The pipe character indicates that you want to send the results of the left hand side to the filter defined on the right side. The filter will modify the value in some way.

The 'escape' filter is just one of many.

The list of built in filters can be found here: Django Documentation - Built-in filters reference

In a django template the | character definitely does not mean the 'bitwise OR' operator.

bskinnersf
  • 3,090
  • 1
  • 20
  • 10
10

obj.property|escape is the way to apply the escape filter in a template, which will HTML escape the string representation of that property.

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
  • so, something.property|escape == escape(something.property) ? –  Jan 06 '09 at 17:00
  • Hmm - glad this answer helped, but technically this isn't python. – Kenan Banks Jan 06 '09 at 17:03
  • Dima, only in a template. And equal only in the semantic sense, I'm not sure (may be possible, because my Django is rusty) you can actually write it like "escape(something.property)". – Vinko Vrsalovic Jan 06 '09 at 17:14
  • Please be more careful when editing the question — a lot of the answers to the original question ("…mean in Python") are getting downvoted because they no longer look relevant. – Ben Blank Jan 06 '09 at 19:02
  • No, you can't rewrite it in a function-calling style in a Django template. You must use the pipe. Django templates != Python code. And you can't just use any Python function this way, it has to be registered as a template filter in a template library loaded in that template. – Carl Meyer Jan 06 '09 at 21:50
  • The correct name for it is "built-in template filter". It is not equivalent to a function call. But you can sort of understand it this way. The big difference is that you have to use the list of filters django provide and use the syntax defined by django. I don't count customised filter. That will be another story – Diansheng Oct 16 '17 at 08:46
-3

It's a bitwise "or". It means escape if the property doesn't exist/is null.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794