Django找不到我的媒体文件(在开发服务器上) [英] Django cannot find my media files (on development server)
问题描述
我已经尝试阅读了很多已经被问及的问题和可用的文档。
I have tried to read up a lot of the questions that have already been asked and the documentation that's available.
媒体目前在我的本地开发机器上。
The media is currently on my local development machine.
我的MEDIA_ROOT,MEDIA_URL,ADMIN_MEDIA_PREFIX,具体如下:
My MEDIA_ROOT, MEDIA_URL, ADMIN_MEDIA_PREFIX and are specified as below:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), "media")
MEDIA_URL = '/media/'
SITE_URL = 'http://localhost:80'
ADMIN_MEDIA_PREFIX = '/media/admin/'
没有admin文件夹,但应该没有区别我不认为。
在urls.py文件中,我有:
There is no 'admin' folder but that shouldn't make a difference I don't think. In the urls.py file I have:
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
我很失望,我应该做些什么来让它工作。
[我正在尝试学习django,并且正在使用一个非常漂亮的现有项目)
I am at a loss as to what I should do to get it working. [I am trying to learn django and am working with an existing project that's pretty hairy]
推荐答案
混合和匹配前后Django 1.3静态文件处理。原来所有静态文件都是从 MEDIA_URL
提供的,但是Django 1.3引入了静态文件的contrib包和相关的 STATIC_ROOT
和 STATIC_URL
设置。 django.views.static.serve
使用您尚未设置的新的staticfiles应用程序。
You're mixing and matching pre and post-Django 1.3 static file handling. Originally all static files were served from MEDIA_URL
, but Django 1.3 introduced the staticfiles contrib package and the associated STATIC_ROOT
and STATIC_URL
settings. django.views.static.serve
utilizes the new staticfiles app, which you haven't set up.
假设您正在运行Django 1.3,首先,您需要在 INSTALLED_APPS
中添加staticfiles。然后,您需要定义 STATIC_ROOT
和 STATIC_URL
。标准位置是名为static的项目根目录。
Assuming you're running Django 1.3, first, you'll need to add 'staticfiles' to your INSTALLED_APPS
. Then, you'll need to define STATIC_ROOT
and STATIC_URL
. The standard location is a project-root level directory named "static".
您还需要添加staticfiles模板上下文处理器:
You'll also need to add the staticfiles template context processor:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
)
这将使您的模板中的 STATIC_URL
变量可用您可以使用类似 {{STATIC_URL}} css / style.css
This will make the STATIC_URL
variable available in your templates, so you can reference your resources with something like {{ STATIC_URL }}css/style.css
的所有资源引用您的资源还需要进入一个名为static的应用程序级目录。实际的项目根级别static目录不会直接使用。这就是 collectstatic
管理命令的所在位置,将转储您所有的静态资源以供生产。
All your static resources will also need to go into an app(s)-level directory named "static". The actual project-root level "static" directory is never directly used. It's simply the place where the collectstatic
management command dumps all your static resources for use in production.
如果你想项目范围的静态资源(不绑定到任何一个特定应用程序),您将需要一个完全独立的目录(即不同于 MEDIA_ROOT
或 STATIC_ROOT
)。我倾向于使用一个名为资产的资产。然后,您需要告诉Django在这里查看静态资源以及 STATICFILES_DIRS
设置:
If you want project-wide static resources (not tied to any one particular app), you'll need an entirely separate directory (i.e. not the same as MEDIA_ROOT
or STATIC_ROOT
). I tend to use one named "assets". You'll then need to tell Django to look in here for static resources as well with the STATICFILES_DIRS
setting:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'assets'), # or whatever you named it
)
MEDIA_ROOT
/ MEDIA_URL
现在只用于用户上传(例如通过
FileField
s和 ImageField
创建的任何文件,所以你仍然需要它,但你不会手动存储任何东西。
MEDIA_ROOT
/MEDIA_URL
are now only used for user uploads (e.g. any file created through FileField
s and ImageField
s, so you still need it, but you won't ever manually store anything there.
当您达到生产时,您的网络服务器将需要同时服务 MEDIA_ROOT
和 STATIC_ROOT
在 MEDIA_URL
和 STATIC_URL
,你还需要运行:
When you reach production, your webserver will need to serve both MEDIA_ROOT
and STATIC_ROOT
at MEDIA_URL
and STATIC_URL
, respectively. You'll also need to run:
$ python manage.py collectstatic
要使Django将所有静态文件编译到由 STATIC_ROOT
指定的目录中。
To make Django compile all your static files into the directory specified by STATIC_ROOT
.
这篇关于Django找不到我的媒体文件(在开发服务器上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!