On 17/02/11 17:44, hank23 wrote:
> Is it possible to use external javascript files with a django app?

Certainly - The django admin itself does it, for example, bundling
a version of jquery and a bunch of stuff in admin/media/js/

> If
> so what folder does the javascript file need to be placed in? 

Patterns followed by reusable apps:

The "new way" is to note the "staticfiles" django app [1] for managing
static files is apparently being rolled into django 1.3 for dealing with
such things.  Stick to its expectations (see its docs), and you could
maybe use the addon for 1.2 [1], then migrate to the 1.3 bundled [2]
version when the time comes.

An "old" way (as seen in the <= 1.2 admin*) withoutusing that app is to
bundle them in some subdirectory distributed as part of the app (say
myapp/static/), then write a custom templatetag (say
{%myapp_static_prefix%}) that reads a setting (say
settings.MYAPP_STATIC) for a url prefix, then tell users of myapp to set
up their web server one way or another to serve out that directory** or
a copy of it somewhere or other, and tell them to set the setting to
point at it wherever they're serving from. So say the user serves out
your apps' static files at
http://static.example.com/blah/myapp/
then they set the setting
MYAPP_STATIC = "http://static.example.com/blah/myapp/";
and your template tag reads that setting, then in myapp templates you can do
{% load myapp_tags %}
<script type="text/javascript" src="{%myapp_static_prefix%}js/something.js">

* Note for the 1.2 admin, compared to the above, the word "media" is
used rather than "static", but then there's plenty of opportunity for
confusion with the way model FileFields work using the MEDIA_ROOT and
MEDIA_URL settings to generate urls for serving back out previously
uploaded files [3], and of course with the form/widget "media" which
also reused MEDIA_URL in 1.2 [4]  ***

** For development purposes only, you can do that internally to
django with django.views.static.serve as per the django docs.

*** AFAIK the idea in 1.3 is that MEDIA_ROOT and MEDIA_URL are all about
the dealing with file uploads and serving them back out, and truly
static stuff is no longer commingled (except for backward compat) using
instead STATIC_ROOT and STATIC_URL.



[1] http://pypi.python.org/pypi/django-staticfiles/
[2] http://docs.djangoproject.com/en/dev/howto/static-files/
[3]
http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.FileField.storage
[4] http://docs.djangoproject.com/en/1.2/topics/forms/media/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to