I just discovered that I took down an environment with one call to
`static` from `django.contrib.staticfiles.templatetags.staticfiles`.
{{{
from django.contrib.staticfiles.templatetags.staticfiles import static
class FooForm(ModelForm):
class Media:
js = (
static("foo.js"),
)
}}}
`foo.js` didn't get put in place.
So what is the correct way to reference a static file outside of a
template keeping something like
`django.contrib.staticfiles.storage.ManifestStaticFilesStorage` in mind?
--
Ticket URL: <https://code.djangoproject.com/ticket/23506>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Something simple like this even:
{{{
from django.contrib.staticfiles.templatetags.staticfiles import static as
_static
import logging
logger = logging.getLogger(__name__)
def static(path):
try:
return _static(path)
except:
logger.exception('Exception raised looking for static file "%s"' %
path)
return u"/missing-file/%s" % path.lstrip("/")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23506#comment:1>
* status: new => closed
* resolution: => wontfix
Comment:
Such a function doesn't belong to Django. We aren't going to use a bare
`except` and rely on logging instead of raising exceptions.
Referencing a static file that doesn't exist is a bug. The proper behavior
is to raise an exception in that case. Silencing errors isn't "safe" or
"correct".
That said, your last question points out that there's no documented way to
accessing the URL of static files. For the sake of clarity, I've filed
#23563 specifically about that problem.
--
Ticket URL: <https://code.djangoproject.com/ticket/23506#comment:2>
Comment (by thenewguy):
Hello,
I understand a bare except clause would not be the final result.
But is it really best for a missing static file in a form to bring down an
entire site?
Maybe the exception could be delayed until the form is rendered for
missing static files. I can understand raising an exception on the page
that uses it but preventing the entire application from loading seems a
little extreme.
Sorry for any typos. Typed on my cell phone.
Thanks
--
Ticket URL: <https://code.djangoproject.com/ticket/23506#comment:3>
Comment (by aaugustin):
That's entirely up to you. Have your code call `static()` at run time, not
at compile time.
--
Ticket URL: <https://code.djangoproject.com/ticket/23506#comment:4>