Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "load url from future" in Django

Tags:

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?

like image 872
lakshmen Avatar asked Jun 26 '12 10:06

lakshmen


2 Answers

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.

like image 73
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 06:09

Ignacio Vazquez-Abrams


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.

like image 41
Lorenz Lo Sauer Avatar answered Sep 21 '22 06:09

Lorenz Lo Sauer