0

I'd like to compute the argument to a tag like this:

{% static /css/{{my_variable_here}}/rest/of/path.css %}

Is this possible? Or do I need to compute the string in the view?

Using django 1.4 (don't question it).

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • Is django templating a hard requirement? I ask because I know this is certainly possible with Mako, but I'm unsure about django's default template. – That1Guy Sep 03 '14 at 18:14
  • @That1Guy Yep, changes in the technology are not on the table. – Marcin Sep 03 '14 at 19:02
  • 1
    Would the suggestions from [this question](http://stackoverflow.com/questions/4386168/how-to-concatenate-strings-in-django-templates) work, using the `add` filter? – Paulo Almeida Sep 03 '14 at 22:23
  • @PauloAlmeida Very cool! (Although kind of an abuse) – Marcin Sep 03 '14 at 22:48
  • Yeah, I thought it was a bit of a hack too, but it's not an easy problem to solve elegantly. You can't create a tag, because you want to use `static`, and a custom filter wouldn't be much cleaner than `add` (if at all). Using `STATICFILES_DIRS`, or the static files loaders, might be an option, but I've never looked into that so I have no idea. – Paulo Almeida Sep 03 '14 at 23:07
  • 1
    `Using django 1.4 (don't question it)` - hah I am still supporting 1.3 – Burhan Khalid Sep 04 '14 at 06:01

1 Answers1

0

I recall having read that putting a template variable inside a tab is a definite no-go. You could perhaps write a custom tag that took your my_variable_here value, just as the built-in "url" tag takes arguments.

But early on I found stackoverflow suggestions about how to resolve some of the usual static files issues that led me to do things this way:

<link href="{{STATIC_URL}}css/bootstrap.min.css" rel="stylesheet">

The drawback is of course that with every view that needs to use the static file I have to do something like this:

return render_to_response('some_page.html', {'STATIC_URL': settings.STATIC_URL})

And "settings" is the project settings.py file and it must be imported.

So obviously with that scheme you could add your {{my_variable_here}} variable as you please.

Edit of 9/6/14 started here (I'm having to respond to Marcin's first comment here so as to show some code). @Marcin, I don't know for sure what you mean by portability across environments. Under the simple circumstances of going from a Linux development machine to a Linux shared-hosting production server I'm having no such problems. I can simply download a copy of the production server setup and put it in any directory of the development server and, after launching the virtualenv, run the project on the development server--- after changing one boolean in the settings.py file:

IS_PRODUCTION = False
DEBUG = True
if IS_PRODUCTION: DEBUG = False
TEMPLATE_DEBUG = DEBUG
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PUBLIC_HTML = os.path.dirname(BASE_DIR)
if IS_PRODUCTION:
    EMAIL_BACKEND = 'django_sendmail_backend.backends.EmailBackend'
else:
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
if IS_PRODUCTION: ALLOWED_HOSTS = [ u'www.mydomainname.com', u'mydomainname.com']
if not DEBUG and not IS_PRODUCTION: ALLOWED_HOSTS = [ u'127.0.0.1', u'127.0.0.1:8000' ]
STATIC_ROOT = os.path.join(BASE_DIR, 'public/static/')
if IS_PRODUCTION:
    STATIC_URL = 'http://www.mydomainname.com/rb/public/static/'
else:
    STATIC_URL = os.path.join(BASE_DIR, 'public/static/')
STATICFILES_DIRS = (
    os.path.join(PUBLIC_HTML, 'dygraph'),
    os.path.join(PUBLIC_HTML, 'ico'),
    os.path.join(PUBLIC_HTML, 'jquery'),
    .... etc.
)

So with that approach I specify directories from which collectstatic is to collect files into public/static. That is the extent to which I care to rely on Django's automated static file arrangements. Note that I only have to change the boolean value in the first line to go from development to production server (provided that I leave the STATICFILES_DIRS in the same directory relationship to the project root folder).

But... I have an answer that will probably be more to your liking. That is, I've found that your question was previously answered on another stackoverflow page. Note that @rounin on Mar 13th pointed to the django docs but I'll provide the exact docs for your miserable version 1.4.

I'll be looking into changing my own code so as to make use of {% get_static_prefix as STATIC_PREFIX %} instead of transmitting STATIC_PREFIX in a response. I submit that this covers the topic.

Community
  • 1
  • 1
Mike O'Connor
  • 2,494
  • 1
  • 15
  • 17
  • Thanks for this, but the problem I'm solving is that this method is broken - it's not reliably portable across environments. Also, you can configure Django to always supply the static root in the environment: https://docs.djangoproject.com/en/1.4/howto/static-files/ – Marcin Sep 04 '14 at 13:21
  • @Marcin, as far as I know STATIC_ROOT is just the directory into which collectstatic collects; STATIC_URL is a URL (unless you're on the development server in which case you could call it a directory). Anyway, I just posted an edit that cites a previous stackoverflow thread--- I just found it--- that may be to your liking. Here's the related django docs reference [https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#std:templatetag-get_static_prefix]. When all's said and done that approach appears to be what I had suggested (`STATIC_PREFIX` appears to be the same as `STATIC_URL`). – Mike O'Connor Sep 06 '14 at 23:02
  • See this article for the problem with this approach: http://staticfiles.productiondjango.com/blog/stop-using-static-url-in-templates/ – Marcin Sep 07 '14 at 14:11
  • If you go to the linked-to docs in my previous comment, take the ] off of the end of the URL in the location box to get to `get_static_prefix`. Then, right above it, see that there's a chance to replace my `load static` with `load static from staticfiles`. I'm talking about the "Note" insert. I can't find an example in the docs of `get_static_prefix` following a load from `staticfiles', but it looks as though it would work anyway. See also `user_stylesheet` passed as a variable, without {{}}, just above the Note box. My substituting `STATIC_URL` for `STATIC_PREFIX` was harmless, I think. – Mike O'Connor Sep 08 '14 at 17:23
  • Strike the last sentence of the comment above please (I was stupidly talking about my own code, not about what I wrote above) @Marcin, should this fail maybe you need to rewrite the static tag so as to take your values and register it as a custom tag (as I had first suggested). – Mike O'Connor Sep 08 '14 at 17:39
  • I have found another approach [http://stackoverflow.com/questions/4386168/how-to-concatenate-strings-in-django-templates]. – Mike O'Connor Sep 08 '14 at 22:56