Django:提供ADMIN媒体文件 [英] Django: serving ADMIN media files

查看:151
本文介绍了Django:提供ADMIN媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地为媒体文件提供正常的媒体文件,但是当我尝试提供管理媒体文件时,我失败了。请帮助我查找问题,因为我已经尝试解决问题已经几个小时了,没有运气(googling也读取django文档关于服务静态文件)。



我尝试访问localhost:8000 / media / a.gif的错误如下:


页面不发现:
f:\python25\lib\site-packages\django / contrib / admin / media\a.a.gif


我将管理媒体文件放在名为media的目录中,而将普通媒体文件放在名为static的目录中。我也在Windows上。



以下是我在urls.py中提供普通媒体文件的方法:

 #提供静态文件
从django.conf导入设置
如果settings.ENVIRONMENT == settings.ENV_DEVELOPMENT:
urlpatterns + = patterns(django。视图,
url(r%s(?P< path>。*)$%settings.MEDIA_URL [1:],static.serve,{document_root:settings.MEDIA_ROOT,})

我的settings.py(只有重要的部分):

  import project_path 
MEDIA_ROOT = project_path.MEDIA.replace('\\','/')
MEDIA_URL ='/ static /'
ADMIN_MEDIA_PREFIX ='/ media /'
TEMPLATE_DIRS =(
project_path.TEMPLATE.replace('\\','/'),

我的project_path.py:

 导入sys 
从os.path导入dirname,加入
ROOT = dirname(__ file__)
APP = join(ROOT,apps)
TE MPLATE = join(ROOT,templates)
MEDIA = join(ROOT,static)
ADMIN_MEDIA = join(ROOT,media)
/ pre>

任何提示?



或者至少可以分享如何为您的管理媒体文件不要从Web服务器中更改任何文件,而只能通过django源代码)



提前感谢:)

ADMIN_MEDIA_PREFIX 显式设置域
,否则runserver命令会将管理员媒体文件从contrib.admin。



我也被这种魔术行为烧毁了。有一张机票( Ticket#8336 ),但设计决定是为了方便和/或

所以要从您的目录中的$ code为您的管理媒体(要使用grappelli或任何要使用的管理皮肤) > runserver 命令你必须使用以下东西:

  MEDIA_ROOT = os.path.join(PROJECT_ROOT ,'media /')
ADMIN_MEDIA_ROOT = os.path.join(PROJECT_ROOT,'admin-media /')
MEDIA_URL ='/ site-media /'
ADMIN_MEDIA_PREFIX ='http: localhost:8000 / admin-media /'

我希望在这里复活正确的问题。提前道歉。


I've been successfully serving media files for the normal MEDIA files, but when I tried serving admin media files, I failed. please kindly help me locating the problem, as I've tried to troubleshoot the problem for several hours already with no luck (been googling too and read the django doc about serving static files as well).

The error as I tried to access localhost:8000/media/a.gif is as following:

Page not found: f:\python25\lib\site-packages\django/contrib/admin/media\a.gif

I put the admin media files in directory named "media", while I put the normal media files in directory named "static". I'm on Windows, too.

Here's how I serve the ordinary media files in urls.py:

# serve static files
from django.conf import settings
if settings.ENVIRONMENT==settings.ENV_DEVELOPMENT:
    urlpatterns += patterns("django.views",
        url(r"%s(?P<path>.*)$" % settings.MEDIA_URL[1:], "static.serve", {"document_root": settings.MEDIA_ROOT,})
    )

And my settings.py (only the important pieces):

import project_path
MEDIA_ROOT = project_path.MEDIA.replace('\\','/')
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'
TEMPLATE_DIRS = (
    project_path.TEMPLATE.replace('\\','/'),
)

And my project_path.py:

import sys
from os.path import dirname, join
ROOT = dirname(__file__)
APP = join(ROOT, "apps")
TEMPLATE = join(ROOT, "templates")
MEDIA = join(ROOT, "static")
ADMIN_MEDIA = join(ROOT, "media")

Any hints?

or maybe at least please share how do you serve your admin media files (without changing any files from the web server, but only via the django source code)

Thanks in advance :)

解决方案

Your answer is that unless ADMIN_MEDIA_PREFIX explicitly sets a domain the runserver command will serve out the admin media files from contrib.admin.

I got burned by this magic behaviour, too. There was a ticket for this (Ticket #8336), but the design decision was to leave the convenience and confusion as it is.

So to serve your admin media (for using grappelli or whatever admin skin you want to use) from your directories with the runserver command you have to use something like:

MEDIA_ROOT =  os.path.join(PROJECT_ROOT, 'media/')
ADMIN_MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'admin-media/')
MEDIA_URL = '/site-media/'
ADMIN_MEDIA_PREFIX = 'http:/localhost:8000/admin-media/'

I hope I am resurrecting the correct question here. Apologies in advance.

这篇关于Django:提供ADMIN媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆