Задать вопрос

Тел: +7 965 3737 888

517

Просмотров

2

Ответов

Serve static media and indexes from app directories [Python2.5, Development only]

<p>This view serves static media and directory indexes for a django application. It should only be used in development, media should be provided directly by a web server in production.</p>
<p>This view assumes a django application stores its media in app/media (which is very common) and the file is referred to in the templates by the last part of a django app path. e.g. As in django.contrib.admin -&gt; 'admin'.</p>
<p>First we check if the media is a request in an application directory; if so we attempt to serve it from there. Then we attempt to provide the document from the document_root parameter (if provided).</p>
<p>To use this view you should add something like the following to urls.py: 
if settings.DEBUG:
        urlpatterns += (r'^media/(?P&lt;path&gt;.*)$', 'site.media.serve_apps', {'document_root' : settings.MEDIA_ROOT})
You can then have the admin media files served by setting ADMIN_MEDIA_PREFIX = '/media/admin/'</p>

Вопрос полезен? Да0/Нет0
file_5199.py(2.9Кб)
None

Ответы (2):

Ответgoldan:19.05.2009
Ответ полезен? Да0/Нет0

Thank you for a great snippet. It saved hours for me)

To make it work for me, I had to make some corrections (maybe the reason is that I'm on Windows):

  1. line 31: replace 'os.sep' with '"/"'. Correct me if I'm wrong, but since 'path' is taken from url, it always contains forward slashes.

  2. line 40: replace 'run_module' with '__import__'. The former just doesn't work for me (maybe because of Windows again)

  3. line 41: replace all with 'moddir = mod.'__path__[0]'. This follows from the previous correction.

I've also made some adjustments to the script according to my pattern of naming media folders. I store media in app/media/media_type/app/ folders, e.g. js files in app/media/js/app/ etc. If someone uses the same pattern and needs these adjustments, feel free to ask me.

Ответantmangaka:07.04.2009
Ответ полезен? Да0/Нет0

hello, I get an internal server error 'str' object has no attribute 'resolve'