Reputation: 2709
There is a Django template, I want to use instance.links[0]['href'].split(':')[1]
as a variable, and use a with
tag to invoke it:
{% if instance %}
{% with "http:{{ instance.links[0]['href'].split(':')[1] }}/dashboard/speed?speed=" as url%}
{% endwith %}
{% endif %}
And this is instance
:
def set_network_speed(instance):
template_name = 'project/instances/set_network_speed.html'
context = {"instance": instance}
return template.loader.render_to_string(template_name, context)
The above code is wrong. Could someone help me to fix it? Thanks a lot !
Upvotes: 0
Views: 97
Reputation: 6467
The best way for you is to add an empty args method on your instance object class.
class InstanceClass(AnyThing):
def get_url(self):
return "http://" + instance.links[0]['href'].split(':')[1] + "/dashboard/speed?speed="
Then you can use it in the template
{% if instance %}
{% with instance_url=instance.get_url %}
{% endwith %}
{% endif %}
edit: Thanx for the comment Quentin Pradet. Fix can be implemented in the question too.
Even if it will certainly not conflict in templates, I would not use an existing command name as var name to avoid confusion.
Upvotes: 1
Reputation: 599778
No. Your can't do that in a template. Even if you did the rest of the syntax, you can't pass an argument to split
.
You will need to write a custom template tag or filter.
Upvotes: 1