1. Germano Gabbianelli
  2. django-autocomplete
Issue #21 open

Use django.contrib.staticfiles to handle static media

Fabian Büchler
created an issue

It would be great if autocomplete could make use of Django's staticfiles framework.

http://docs.djangoproject.com/en/dev/howto/static-files/

I think the proposed behaviour would be to collect all files (css/images/js) under a "static" folder within your app like that:

{{{ /autocomplete /static /autocomplete /css /images /js /jquery.ui.js /... }}}

This would make AUTOCOMPLETE_MEDIA_PREFIX obsolete and I think users could just overwrite certain files by replicating the same structure in their project's static media folder.

To use the pathes in templates you just need to add a template context processor:

{{{

!python

TEMPLATE_CONTEXT_PROCESSORS = ( ... 'django.core.context_processors.static', ... ) }}}

while in your templates you could then use:

{{{

!django

<img src="{{ STATIC_URL }}autocomplete/js/jquery.ui.js" /> }}}

Let me know if I can help with the realization of this.

Regards, Fabian

Comments (7)

  1. Fabian Büchler reporter

    Hi, Germano,

    answering your question about backwards compatibility here:

    basically, the result of my research is as follows:

    • restructure your media directory as outlined in my ticket. (static/js, static/css, ...
    • for Django >= 1.2.4, set your AUTOCOMPLETE_MEDIA_URL to STATIC_URL + '/autocomplete/' automatically (e.g. in your app's init.py).
    • for Django <= 1.2.3, users need to set the AUTOCOMPLETE_MEDIA_URL manually. (They need to copy the media files anyway).
    • make the AUTOCOMPLETE_MEDIA_URL setting optional for Django 1.2.4+.
    • once django.contrib.staticfiles is widely used, you can remove the AUTOCOMPLETE_MEDIA_URL setting completely and just use settings.STATIC_URL or STATIC_URL instead.

    Hope this is helpful... Regards, Fabian

  2. Jannis Leidel

    Hi there, staticfiles author here, I think you got something wrong. staticfiles will be part of the upcoming Django 1.3 release, *not* 1.2.4.

    I'd like to propose a different migration path:

    • Move the files to static/autocomplete/ in the the app directory.
    • Tell Django >= 1.3 users to use django.contrib.staticfiles
    • Tell Django < 1.3 users to use the standalone version of staticfiles (http://pypi.python.org/pypi/django-staticfiles) which is compatible to the app included in Django 1.3 and works on Django 1.1.X-1.3.X

    You don't require an own AUTOCOMPLETE_MEDIA_URL setting since you can just use STATIC_URL + "autocomplete/" in your code.

  3. Fabian Büchler reporter

    Hi Jannis, awesome - thanks for clarification about versions! My approach was not to require django-staticfiles for pre 1.3 users. But just requiring it is also an option, of course. Regards, Fabian

  4. Germano Gabbianelli repo owner

    I moved static files to static/autocomplete/ and replaced all occurrences of AUTOCOMPLETE_MEDIA_PREFIX with STATIC_URL + "autocomplete/".

    The only required setting to use django-autocomplete would become STATIC_URL.

    Users of Django >= 1.3 would be able to use contrib.staticfiles with autocomplete.

    Users of Django < 1.3 would be forced to serve the static files in autocomplete/static from STATIC_URL.

  5. Anonymous

    For the unaware you will also have to add autocomplete to INSTALLED_APPS in order for staticfiles to find the css/images/js files.

  6. Log in to comment