Re: Calling a function for a url tag parameter

12 views
Skip to first unread message

Aubrey Stark-Toller

unread,
Oct 31, 2014, 11:24:02 PM10/31/14
to django...@googlegroups.com
Hey,
 
A filter would work better here if you can use one. The following should work:
 
Defined as:
@register.filter
def encode_url(link_text):
    return link_text.replace(' ', '_')
 
And in template:
{% with state_url=flow.state.description|encode_url %}
<a href="{% url 'advance-flow' flow.id state_url %}">advance</a>
{% endwith %}
 
Aubrey
 
 
On Thu, Oct 30, 2014, at 03:23 PM, Daniel Grace wrote:
Hi,
I am trying to use the url tag on some parameters, one of which needs to come from a function.
For example as follows:
{% with state_url=encode_url flow.state.description %}
<a href="{% url 'advance-flow' flow.id state_url %}">advance</a>
 
encode_url is defined as follows:
@register.simple_tag
def encode_url(link_text):
    return link_text.replace(' ', '_')
 
This causes an error on the with statement:
Request Method:GET
Django Version:1.7
Exception Type:TemplateSyntaxError
Exception Value:
'with' received an invalid token: 'flow.state.description'
 
I'm guessing that you cannot put another template tag in the "with" statement.  I cannot see a way around this.  Any ideas?
 
Thanks
 
 
Reply all
Reply to author
Forward
0 new messages