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).
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).
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.