[Django] #23506: How to reference static files outside of the template

12 views
Skip to first unread message

Django

unread,
Sep 17, 2014, 5:38:45 PM9/17/14
to django-...@googlegroups.com
#23506: How to reference static files outside of the template
-------------------------------------+--------------------
Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.staticfiles | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+--------------------
There should be a function like
`django.contrib.staticfiles.templatetags.staticfiles.static` that is safe
to use outside of a template. When it cannot find a static file, it
should probably be configured to return a url pointing at a view that logs
the file is missing and returns a 404.

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.

Django

unread,
Sep 17, 2014, 5:54:43 PM9/17/14
to django-...@googlegroups.com
#23506: How to reference static files outside of the template
-------------------------------------+-------------------------------------

Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.staticfiles | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by thenewguy):

* 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>

Django

unread,
Sep 28, 2014, 5:34:34 AM9/28/14
to django-...@googlegroups.com
#23506: How to reference static files outside of the template
-------------------------------------+-------------------------------------
Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.staticfiles | Version: 1.7
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* 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>

Django

unread,
Sep 29, 2014, 9:24:57 AM9/29/14
to django-...@googlegroups.com
#23506: How to reference static files outside of the template
-------------------------------------+-------------------------------------
Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.staticfiles | Version: 1.7

Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 29, 2014, 9:49:22 AM9/29/14
to django-...@googlegroups.com
#23506: How to reference static files outside of the template
-------------------------------------+-------------------------------------
Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.staticfiles | Version: 1.7

Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages