Add an extra parameter to 'static' tag

158 views
Skip to first unread message

Renato Oliveira

unread,
May 31, 2014, 9:26:06 PM5/31/14
to django-d...@googlegroups.com
Hi all,

My name is Renato Oliveira and I'm a Brazilian Django user.

Here at my company we use a small modification of static template tag.

We added a parameter, URL, and the inside job is when DEBUG is True, the tag returns the local path for the static file, but when DEBUG is false, the URL is returned.

So, why do we use it. We do a lot of projects using staticfiles that are already online, at CDNs for example, and there is no need for me to serve it. But when I'm writing code, I don't want useless requests (my internet is not so fast) and also, I want to be able to work offline. Also, if everyone who uses bootstrap, foundation and that kind of stuff, point to the same file, they will already be on cache, so a lot of happy people.

I open sourced the modification we did (4 or 5 lines plus tests), and was about to send it to pypi and wondered if it is an improvement to do on Django itself.

Of course, I would need some help to find the best way to do it on Django. In our version, we just made it works.

Best regards,

Renato Oliveira

Renato Oliveira

unread,
May 31, 2014, 9:55:36 PM5/31/14
to django-d...@googlegroups.com
Sorry if I gave the wrong impression, this e-mail isn't to publish a new project, It's a feature proposal.



Renato Oliveira


Florian Apolloner

unread,
Jun 1, 2014, 5:52:30 AM6/1/14
to django-d...@googlegroups.com
Hi Renato,


On Sunday, June 1, 2014 3:26:06 AM UTC+2, Renato Oliveira wrote:
I open sourced the modification we did (4 or 5 lines plus tests), and was about to send it to pypi and wondered if it is an improvement to do on Django itself.

I don't think so, since Django already provides a way to do what you are after: You can write your on static storage backend which the static-tag will then use, this way the backend will figure out what to do when DEBUG=False/True.

Cheers,
Florian

Renato Oliveira

unread,
Jun 1, 2014, 10:36:55 AM6/1/14
to django-d...@googlegroups.com


On Sun, Jun 1, 2014 at 6:52 AM, Florian Apolloner <f.apo...@gmail.com> wrote:
Hi Renato,

Hi Florian, thanks for answering 


On Sunday, June 1, 2014 3:26:06 AM UTC+2, Renato Oliveira wrote:
I open sourced the modification we did (4 or 5 lines plus tests), and was about to send it to pypi and wondered if it is an improvement to do on Django itself.

I don't think so, since Django already provides a way to do what you are after: You can write your on static storage backend which the static-tag will then use, this way the backend will figure out what to do when DEBUG=False/True.

Yeah, i'm aware of this, and sorry for not being explicit. The goal of this improvement is to point to many places. For exampe, if I have a project with, bootstrap, jquery and angular. I can do this:

{% static 'path/to/my/angular.js' http://googlecdn/angular.js %}
{% static 'path/to/bootstrap.css' http://bootstrapcdn/bootstrap.css %}

And If I have to write some css or js file,

{% static 'path/to/users.js' %}

And this would use Django storage or S3, whatever.

Cheers,
Florian

I looked for alternatives, and read about django custom storage backends, and it doesn't seemed to me that it could do it.

If there is a way to do this without changing the template tag, this e-mail must be on django-users, sorry :)
 

Reinout van Rees

unread,
Jun 1, 2014, 5:59:55 PM6/1/14
to django-d...@googlegroups.com
On 01-06-14 16:36, Renato Oliveira wrote:
>
> {% static 'path/to/my/angular.js' http://googlecdn/angular.js %}
> {% static 'path/to/bootstrap.css' http://bootstrapcdn/bootstrap.css %}

It seems to me that the core use case of the staticfiles app is to
manage *local* css files well. This includes the scenario where the
local files are copied off to a CDN. That CDN's base URL can then be
used in production.


What you are proposing sounds to be a different problem:

- You want to use a CDN in production.

- You want to use local files as a fallback for when you're on a train
without an internet connection to the CDN.


So... I don't think this change should go into staticfiles.

But... the basic use case is sound. Perhaps you could change your code
into a template tag that looks like
{% cdn http://googlecdn/whatever.js %} ?

With a management command that would download all the relevant files to
a local directory? (Assuming you can list the templates). Or that would
do it on the fly when an internet connection is available when you use
the template?

It works the other way around from your current approach, but that looks
to me to be the right way around :-)



Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

Florian Apolloner

unread,
Jun 1, 2014, 7:26:15 PM6/1/14
to django-d...@googlegroups.com
Hi Renato,


On Sunday, June 1, 2014 4:36:55 PM UTC+2, Renato Oliveira wrote:
On Sunday, June 1, 2014 3:26:06 AM UTC+2, Renato Oliveira wrote:
I open sourced the modification we did (4 or 5 lines plus tests), and was about to send it to pypi and wondered if it is an improvement to do on Django itself.

I don't think so, since Django already provides a way to do what you are after: You can write your on static storage backend which the static-tag will then use, this way the backend will figure out what to do when DEBUG=False/True.

Yeah, i'm aware of this, and sorry for not being explicit. The goal of this improvement is to point to many places. For exampe, if I have a project with, bootstrap, jquery and angular. I can do this:

{% static 'path/to/my/angular.js' http://googlecdn/angular.js %}
{% static 'path/to/bootstrap.css' http://bootstrapcdn/bootstrap.css %}

I don't see any difference to the  DEBUG example here, a storage backend can still return different URLs, depending on the filename (eg angular vs bootstrap). Your proposal seems to remove the choice from the user and puts it into the template. FWIW, in my experience per project CDNs often introduce greater latency and less performance than a single (or more) keep-alive connections to one server fetching all the files (possible concatenated).

Cheers,
Florian

Renato Oliveira

unread,
Jun 2, 2014, 6:45:52 PM6/2/14
to django-d...@googlegroups.com
Thanks for the answers!





Renato Oliveira


--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/d7da3542-f64e-4aed-83c0-4d56070d9d01%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Shai Berger

unread,
Jun 16, 2014, 11:12:49 AM6/16/14
to django-d...@googlegroups.com
Hi Renato,

Sorry for being a little late to this party.

On Sunday 01 June 2014 17:36:43 Renato Oliveira wrote:
>
> Yeah, i'm aware of this, and sorry for not being explicit. The goal of this
> improvement is to point to many places. For exampe, if I have a project
> with, bootstrap, jquery and angular. I can do this:
>
> {% static 'path/to/my/angular.js' http://googlecdn/angular.js %}
> {% static 'path/to/bootstrap.css' http://bootstrapcdn/bootstrap.css %}
>
> And If I have to write some css or js file,
>
> {% static 'path/to/users.js' %}
>
> And this would use Django storage or S3, whatever.

I suspect that your proposal could be acceptable, if, instead of adding HTTP
sources into the templates, you added a "using=" argument that referenced a
file-storage object -- similar to the one on the cache tag.

The problem with this idea is that, while caches are named in a settings
entry, there is currently no such "registry" of file-storage objects. One could
arguably be added to staticfiles' AppConfig, but that will probably only happen
if there are more uses for named storages.

Anyway, doing it this way addresses Florian's concerns -- control of which
named storage goes where stays at the project level, the templates only
specify grouping of files; and I think it gives most of what you want.

The only concern I have about it is, it may be over-engineered -- in practice,
you don't usually have whole sets of files in different storages, and if you do,
you can probably tell the right storage by path; so a simpler solution is a
custom storage (as Florian pointed) which makes all the decisions.

YMMV,
Shai.
Reply all
Reply to author
Forward
0 new messages