120π
It is possible and fairly simple.
Django only allows one argument to your filter, but thereβs no reason you canβt put all your arguments into a single string using a comma to separate them.
So for example, if you want a filter that checks if variable X is in the list [1,2,3,4] you will want a template filter that looks like this:
{% if X|is_in:"1,2,3,4" %}
Now we can create your templatetag like this:
from django.template import Library
register = Library()
def is_in(var, args):
if args is None:
return False
arg_list = [arg.strip() for arg in args.split(',')]
return var in arg_list
register.filter(is_in)
The line that creates arg_list is a generator expression that splits the args string on all the commas and calls .strip() to remove any leading and trailing spaces.
If, for example, the 3rd argument is an int then just do:
arg_list[2] = int(arg_list[2])
Or if all of them are ints do:
arg_list = [int(arg) for arg in args.split(',')]
EDIT: now to specifically answer your question by using key,value pairs as parameters, you can use the same class Django uses to parse query strings out of URLβs, which then also has the benefit of handling character encoding properly according to your settings.py.
So, as with query strings, each parameter is separated by β&β:
{{ attr.name|replace:"cherche=_&remplacement= " }}
Then your replace function will now look like this:
from django import template
from django.http import QueryDict
register = template.Library()
@register.filter
def replace(value, args):
qs = QueryDict(args)
if qs.has_key('cherche') and qs.has_key('remplacement'):
return value.replace(qs['cherche'], qs['remplacement'])
else:
return value
You could speed this up some at the risk of doing some incorrect replacements:
qs = QueryDict(args)
return value.replace(qs.get('cherche',''), qs.get('remplacement',''))
35π
It is more simple than you think
You can use simple_tag for this.
from django import template
register = template.Library()
@register.simple_tag
def multiple_args_tag(a, b, c, d):
#do your stuff
return
In Template:
{% multiple_args_tag 'arg1' 'arg2' 'arg3' 'arg4' %}
NOTE: Donβt forget to re-run the server.
- [Django]-Django-celery: No result backend configured
- [Django]-Images from ImageField in Django don't load in template
- [Django]-How to execute a Python script from the Django shell?
31π
Itβs easy like this.
@register.filter(name='one_more')
def one_more(_1, _2):
return _1, _2
def your_filter(_1_2, _3)
_1, _2 = _1_2
print "now you have three arguments, enjoy"
{{ _1|one_more:_2|your_filter:_3 }}
- [Django]-Django removing object from ManyToMany relationship
- [Django]-Uwsgi installation error in windows 7
- [Django]-UUID as default value in Django model
21π
Instead of a filter, register your tag as a simple tag. Those can take multiple arguments. The syntax for invoking it will be a little bit different, but thatβs just syntactic sugar changing.
- [Django]-How to resize an ImageField image before saving it in python Django model
- [Django]-How to use pdb.set_trace() in a Django unittest?
- [Django]-Cross domain at axios
17π
Not possible according to this section of the docs:
Custom filters are just Python functions that take one or two arguments:
- The value of the variable (input) β
not necessarily a string. - The value of the argument β this can have a
default value, or be left out altogether.
- [Django]-Saving ModelForm error(User_Message could not be created because the data didn't validate)
- [Django]-What's the purpose of Django setting βSECRET_KEYβ?
- [Django]-Django rest framework lookup_field through OneToOneField
3π
This feature has been marked as WONTFIX in 2013 Djangoβs Trac: http://code.djangoproject.com/ticket/1199
- [Django]-Django β How to use decorator in class-based view methods?
- [Django]-Can't compare naive and aware datetime.now() <= challenge.datetime_end
- [Django]-Django import error β No module named core.management
3π
<my-site>/globaltags/replace.py
from django.template import Library
import re
register = Library()
def search(value, search):
return re.sub(search, '#f4x@SgXXmS', value)
def replace(value, replace):
return re.sub('#f4x@SgXXmS', replace, value)
register.filter(search)
register.filter(replace)
In the template:
{{ "saniel"|search:"s"|replace:"d" }}
- [Django]-Uninstall Django completely
- [Django]-DRF: custom ordering on related serializers
- [Django]-How to get username from Django Rest Framework JWT token
0π
For example, you can use the separator ,
to pass multiple values to replace
filter, then use the values by spliting with split() as shown below. *You can and should use other separator which values donβt have:
from django import template
register = template.Library()
@register.filter
def replace(value, args):
cherche, remplacement = args.split(',')
return value.replace(cherche, remplacement)
{{ "2023-06-23"|replace:"-,/" }} {% 2023/06/23 %}
{% Here β %}
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
- [Django]-Django β Clean permission table
- [Django]-Django index page best/most common practice
-1π
You can just simply do this:
{% assign find_total_issued = dailysalesreport | find: "TotalIssued":"13" %}
public static List<object> Find(object collection, string column, string value)
And it will reach the destination as the abstraction of the function is sjare
.
- [Django]-How do I reuse HTML snippets in a django view
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-How do I deploy Django on AWS?
-2π
Heres a bad idea but works:
{{ xml|input_by_xpath:"{'type':'radio','xpath':'//result/value'}" }}
and
@register.filter
def input_by_xpath(device, args):
args = eval(args)
...
result = "<input type=\"%s\" value=\"%s\" name=\"%s\"/>"%(args['type'],value,args['xpath'])
return mark_safe(result)
- [Django]-Django admin: How to display the field marked as "editable=False" in the model?
- [Django]-Django-Forms with json fields
- [Django]-How to get the name of current app within a template?