33254

django template filter ,use 2 or more filter like pipe

Question:

I want to use more than one filter on template like below:

value: {{ record.status|cut:"build:"|add:"5" }}

where record.status would be build:n, 0 < n< 100 but I want to add this value a base value 5.

I tried above code, it only take effect on the first filter, so I did not get the value plus 5.

Does django only support one filter? Thanks

Answer1:

First, the answer for your question "Does django only support one filter?" is that Django does support <em>almost unlimited</em> number of chained filters (depends on your platform and ability to write that number of chained filters of course =) . Take some code for example (not proof but it makes sense), it is actually a template '{{ x|add:1|add:1|...10000 in all...|add:1 }}'

>>> from django.template import * >>> t = Template('{{ x|'+'|'.join(['add:1']*10000)+' }}') >>> t.render(Context({'x':0})) u'10000'

Second, please check the template to ensure that you are using built-in version of cut and add; also check the output value after the cut to ensure it can be coerced to int w/o raising exception.<br /> I've just checked and found that even the Django 0.95 supports this usage:

def add(value, arg): "Adds the arg to the value" return int(value) + int(arg)

Answer2:

Chaining filters is supported. If you want to figure why it doesn't work, then what I'd do is:

<ol><li>install ipdb</li> <li>in django/templates/defaultfilters.py, find "def add", and put "import ipdb; ipdb.set_trace()" at the top of the function</li> <li>open the page in the browser again, you should be able to follow the execution of the code from the terminal that runs runserver and figure why you're not getting the expected results</li> </ol>

An easier way is to <a href="https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters" rel="nofollow">make your own template filter</a>. It could look like

from django.template import Library register = Library() @register.filter def cut_and_add(value, cut, add): value = value.replace(cut, '') value = int(value) + add return value

Suppose you saved this in yourapp/templatetags/your_templatetags.py (and that yourapp/templatetags/__init__.py exists - it can be empty). Then you would use it in the template as such:

{% load your_templatetags %} {{ record.status|cut_and_add:"build:",5 }}

Of course, this is untested, pseudo code. But with a little effort you could get it to work.

Recommend

  • How can I test TCP socket status in Perl?
  • awk or sed CSV file manipulation
  • Raise Session_OnStart event from custom ASP.NET SessionStateProvider class
  • Parallelizing random forests
  • Undefined navigator.push React-native 0.43.4
  • Activation Function choice for Neural network
  • redirect_to root_url and return unless @user.activated
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • Android Studio Can't Find tools.jar
  • Filter strings with regex before casting to numeric
  • Disable Kendo Autocomplete
  • Elasticsearch script query involving root and nested values
  • UWP/C# - Issue with AQS and USB Devices
  • Why use database factory in asp.net mvc?
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • How do I configure context broker accept post requests from my remote sensor?
  • Django invalid literal for int() with base 10
  • xtable package: Skipping some rows in the output
  • Ensure fsync did its job
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • C++ Partial template specialization - design simplification
  • Django: Count of Group Elements
  • Recording logins for password protected directories
  • Email format validation in mvc3 view
  • Splitting given String into two variables - php
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Get object from AWS S3 as a stream
  • How to add date and time under each post in guestbook in google app engine
  • Check if a string to interpolate provides expected placeholders
  • Modifying destination and filename of gulp-svg-sprite
  • Javascript Callbacks with Object constructor
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to handle AllServersUnavailable Exception
  • How to get next/previous record number?
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Python: how to group similar lists together in a list of lists?
  • coudnt use logback because of log4j
  • Busy indicator not showing up in wpf window [duplicate]
  • Observable and ngFor in Angular 2