20

When I read django code sometimes, I see in some templates "load url from future". I am not quite sure what this is but I do know it has something to do with URLs. How and when is this load url from future supposed to be used?

lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • What I would also like to see in these answers - when will this tag disappear from future? Django 1.4 is an LTS, and then maybe 1.8 will be another (no promises from Django project, though). When do I need to dig through my application again, this time removing this future import? – Tomasz Gandor Dec 11 '14 at 11:23
  • `url from future` is removed in django 1.9. see https://github.com/sehmaschine/django-grappelli/issues/648 – codescribblr Sep 19 '17 at 15:32

2 Answers2

10

It's due to a change to the url tag enacted in 1.3:

Changes to url and ssi

Most template tags will allow you to pass in either constants or variables as arguments – for example:

{% extends "base.html" %}

allows you to specify a base template as a constant, but if you have a context variable templ that contains the value base.html:

{% extends templ %}

is also legal.

However, due to an accident of history, the url and ssi are different. These tags use the second, quoteless syntax, but interpret the argument as a constant. This means it isn’t possible to use a context variable as the target of a url and ssi tag.

Django 1.3 marks the start of the process to correct this historical accident. Django 1.3 adds a new template library – future – that provides alternate implementations of the url and ssi template tags. This future library implement behavior that makes the handling of the first argument consistent with the handling of all other variables. So, an existing template that contains:

{% url sample %}

should be replaced with:

{% load url from future %}
{% url 'sample' %}

The tags implementing the old behavior have been deprecated, and in Django 1.5, the old behavior will be replaced with the new behavior. To ensure compatibility with future versions of Django, existing templates should be modified to use the new future libraries and syntax.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • @lakesh There is an explanation on [my answer to a related question](http://stackoverflow.com/a/8490469/400691). – meshy Nov 19 '12 at 11:22
  • 3
    ahhh, it means 'load the Django 1.5 implementation of the url tag'. I always thought some crazy nth order decorator function lazy loading type stuff must be going on. somehow the template was able to load a url that didn't yet exist???...did my head in...I was too scared to look at the code. – Michael Bylstra Feb 10 '13 at 14:41
5

I will put this in a separate answer due to the following salient Exception in connection with templates:

If you get a django.core.urlresolvers.NoReverseMatch Exception thrown from within a django template (Django version >1.4) parser, it may just be the usage of {% load url from future %} within the template.

In this case, simply quote the url that is passed to the url-tag. That is {% url someurl %} should become {% url 'someurl' %}. Thanks to Ignacio VA for pointing me in that direction.

Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87