Using Django's built-in yesno filter, I need to insert one of these values:
owner_name
Here's the code I've tried to use in my template:
"Look what {{ is_owner|yesno:"I,{{ owner_name }}" }} created!"
Using the code above causes the following error:
Could not parse the remainder: ':"I,{{ owner_name' from 'is_owner|yesno:"I,{{ owner_name'
So how do I escape a variable inside a filter's argument?
Another thing you can do is concatenate the arguments using the with
template tag and add
filter:
{% with yesno_args="I,"|add:owner_name %}
"Look what {{ is_owner|yesno:yesno_args }} created!"
{% endwith %}
I think this is simpler than having to add a custom filter.
You can create a custom filter to do what you want.
It would look something like {{ is_owner|my_yesno:owner_name }}
With the custom filter
from django.template.defaultfilters import yesno
def my_yesno(value, arg):
yes_no_list = ['I', arg]
return yesno(value, yes_no_list)
This would let you reuse yesno while creating the list in your wrapper my_yesno
. You can alternatively just write your own logic if you want to do something differently as well. Be sure to {% load %}
your custom filter as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With