Correct way to serve static user-uploaded files for django-adminfiles
I am trying to add django-adminfiles to one of my django projects (a zinnia blog).
I am using Django 1.3 with the following configuration :# setttings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(current_dir, 'static_collected')
STATICFILES_DIRS = (
'/path/to/myproject/src/django-adminfiles/adminfiles/media',
...)
# urls.py
urlpatterns += staticfiles_urlpatterns()
urlpatterns += patterns('',
url(r'^adminfiles/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': os.path.join(parent_dir, 'adminfiles')}),)
I then run bin/manage collecstatic
to collect static files (I am using buildout with djangorecipe).
# If I go to the admin interface at /admin/zinnia/entry/2/
[21/Sep/2011 09:39:16] "GET /adminfiles/all/adminfiles/fluxboxwall_jpg_144x150_q85.jpg HTTP/1.1" 404 1886
[21/Sep/2011 09:39:16] "GET /adminfiles/all/adminfiles/DD_jpg_144x150_q85.jpg HTTP/1开发者_运维技巧.1" 404 1859
# If I visit my public located at /blog
[21/Sep/2011 09:36:02] "GET /blog/adminfiles/fluxboxwall.jpg HTTP/1.1" 404 6758
# This file can be accessed through /adminfiles/fluxwall.jpg
[21/Sep/2011 09:36:02] "GET /blog/adminfiles/DD.jpg HTTP/1.1" 404 6731
# This file can be accessed through /adminfiles/DD.jpg
I can make django-adminfiles work by adding slashes in its templates :
templates/adminfiles/uploader.html, line 37, for the thumbnails in the admin interface
<li class="item {{f.content_type}} {{f.sub_type}}" {% if f.is_image %}style="background-image:url(
/{% thumbnail f.upload 144x150 %});"{% else %}{% if f.mime_image %}style="background-image:url({{ f.mime_image }});"{% endif %}{% endif %}>
templates/adminfiles/default.html, line 2, to have my pictures displayed in my public blog
<img src="
/{{ upload.upload.url }}" width="{{ upload.width }}" height="{{ upload.height }}" class="{{ options.class }}" alt="{% if options.alt %}{{ options.alt }}{% else %}{{ upload.title }}{% endif %}" />
However, I think I shouldn't have to hack templates to make django-adminfiles work. I have a feeling that something is wrong in my django configuration but I don't know what.
You have a "small" mess in here IMHO.
From what I undestand if you setup STATIC_URL = '/static/' then this must be the umbrella for all your static files like:
/static/blog/a_public_file.jpg
/static/adminfiles/DD.jpg
/static/other/app/file.jpg
Also remember you can add static files finders https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders and you can already have that setting, sometimes using STATICFILES_DIRS is confusing.
I suggest you to review all your static files setup from scratch before going any further.
精彩评论