为Django提供开发模式的静态文件 [英] Serving static files for development mode in Django
问题描述
settings.py
的相关部分是: / p>
MEDIA_URL ='/ media /'
STATIC_URL ='/ static /'
MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__ file__)+'/ ..'),'media')
STATIC_ROOT = os.path.join(os.path.abspath (os.path.dirname(__ file__)+'/ ..'),'static')
而$ urls.py
:
urlpatterns + = static MEDIA_URL,document_root = settings.MEDIA_ROOT)
urlpatterns + = static(settings.STATIC_URL,document_root = settings.STATIC_ROOT)
所以静态文件现在位于项目文件夹外面的名为 static
的目录中。我验证 STATIC_ROOT
被评估为一个适当的值。我已经检查过该文件夹是否存在。
然而,当我的浏览器指向地址 localhost:8000 / static / js / somefile.js
,我找不到 404页面未找到
,并显示消息'找不到js / somefile.js'。
。你可以提出一些这样的理由吗?
提前感谢
编辑: / strong>
我想我知道问题可能在哪里:事情是在开发模式下Django尝试从所有安装的应用程序的
但是,我已经添加了一些额外的文件到我的 static /
子目录中的STATIC_URL STATIC_ROOT
,这些文件根本没有提供。也许有一些冲突。
编辑(2):
无论如何当我使用 ./ manage.py runserver --nostatic
运行服务器时,它实际上是从 STATIC_ROOT
目录。我该怎么办?问题是,正如我尝试将所有我的模板文件与项目本身分开,我尝试对某些css和js文件执行相同操作。
它不起作用,因为我想要做的不是很明智。
这是应该如何配置的。 settings.py
:
MEDIA_URL ='/ media /'
STATIC_URL ='/ static /'
MEDIA_ROOT ='media.mydomain.com'
STATIC_ROOT ='static.mydomain.com'
STATIC_DIRS =(
os.path.join(os.path.abspath(os.path.dirname(__ file__)+'/ ..'),'static'),
)
所有文件都保留在正确的位置。
I'm having trouble serving static files in development mode in Django. I do know that this is not a setting that should be used in a production server, so don't worry. For now however I'd like to stick to it.
The relevant parts of settings.py
are:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__) + '/..'), 'media')
STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__) + '/..'), 'static')
And of urls.py
:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
So the static files are located for now in the directory named static
right outside the project folder. I verified that STATIC_ROOT
is evaluated to an appropriate value. I've double checked that the folder exists.
However when pointing my browser to the address localhost:8000/static/js/somefile.js
, I get the dreaded 404 Page Not Found
with the message 'js/somefile.js' could not be found.
. Could you please suggest some reasons to this behaviour?
Thanks in advance.
EDIT:
I think I know where the problem may be: The thing is that in development mode Django attempts to look for the files from the STATIC_URL
in the static/
subdirectories of all the installed apps. However I've added some additional files to my STATIC_ROOT
and these are not served at all. Maybe there is some clash.
EDIT (2):
This must be it. When I run the server with ./manage.py runserver --nostatic
it works, that is it actually serves the files from the STATIC_ROOT
directory. What can I do about it? The problem is that just as I try to keep all my template files separate from the project itself I try to do the same with certain css and js files...
It doesn't work, because what I was trying to do wasn't very wise.
That's how it should be configured. settings.py
:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
MEDIA_ROOT = 'media.mydomain.com'
STATIC_ROOT = 'static.mydomain.com'
STATIC_DIRS = (
os.path.join(os.path.abspath(os.path.dirname(__file__) + '/..'), 'static'),
)
All the files remain in place, exactly where they were.
这篇关于为Django提供开发模式的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!