Dynamic URL in Django/HTML -
{% record in records%} string1 = 'views.' + record.field1 + record.field2 <a href ="{%url string1 %}>test this</a> {%endfor%}
is possible in html / django ? need make variable 'string1' dynamically , pass url. request help.
use {% %}
{% "views."|add:record.field1|add:record.field2 string1 %} <a href="{% url string1 %}">test this</a> {% endwith %}
but isn't considered best practice imo since putting logic inside template.
this should handled method on record
model instead, called field_url
or something. way don't violate dry , won't risk else messing url plus you'll chance of cleaning urls before exposing them.
def field_url(self): return "views.{0}{1}".format(self.field1, self.field2)
and called in template
{% record in records %} <a href="{% url record.field_url %}">my url</a> {% endfor %}
Comments
Post a Comment