1👍
If you’re wanting to do things with context variables like this then you should make what you require available in the context rather than trying to manipulate things in the template.
Either add variable from your view or create a context processor if you have variables that you require in lots of places, because through a context processor you can create variables that are always available.
Check out this answer I wrote recently on this; https://stackoverflow.com/a/27797061/1199464
update following your comment
There’s nothing wrong with writing a method on your model to format a string or similar;
class Foo(models.Model):
token = models.CharField(max_length=150)
reference = models.ForeignKey(Reference)
def get_url(self):
url = u'{media_url}{path}'.format(
media_url=settings.MEDIA_URL,
path=self.token
)
return url
Template:
{% for foo in foos %}
<img id="foo_{{ foo.pk }}" src="{{ foo.get_url }}" />
{% endfor %}
And on a sidenote if you’re not too familiar with Django yet, you should use MEDIA_URL
for user uploaded content and STATIC_URL
for content that is yours. You can read more on these here; How can I get the MEDIA_URL from within a Django template?
Django docs; https://docs.djangoproject.com/en/1.7/ref/settings/#media-url