Django STATIC_URL只能在没有斜杠的情况下工作 [英] Django STATIC_URL only works without leading slash
问题描述
Django 1.10在开发中使用runserver
Django 1.10 using runserver in development
由于某些原因,我的静态目录中找不到文件,除非我将其列为:
For some reason, no files can be found in my static directory unless I have it listed as such:
STATIC_URL = 'static/'
if not os.environ.get('DEPLOYED'):
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
但是所有的文档和我之前所做的一切都说它应该是/ static /这使我认为我在别的地方做错了。我正在使用Django的runserver
But all documentation and everything I have done before says it should be /static/ which makes me think I'm doing something wrong elsewhere. I am using Django's runserver
URLS:
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles import views
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
我现在通过输入localhost:8000 / static / index.html
I get to the site now by typing localhost:8000/static/index.html
推荐答案
这是一个简单的误解。我做了尤金推荐的
It was a simple misunderstanding. I did what Eugene recommended
findstatic index --verbosity=2
输出是3个静态文件夹,不是我的静态文件夹。我意识到我对STATIC_ROOT和STATICFILES_DIRS有一个基本的误解。我的静态文件夹的路径不在开发中的STATICFILES_DIRS中。
The output was 3 static folders that were not my static folder. I realized I had a basic misunderstanding of STATIC_ROOT and STATICFILES_DIRS. The path to my static folder was not in STATICFILES_DIRS in development.
我将静态文件夹添加到STATICFILES_DIRS中,我很好。我建议删除了URL引用。我不得不将STATIC_ROOT更改为不是该文件夹...这是有道理的,因为收藏者将所有内容移动到STATIC_ROOT文件夹进行部署。
I added the static folder to STATICFILES_DIRS and I was good. I removed the URL reference as suggested. I had to change STATIC_ROOT to not be that folder... which makes sense because a collectstatic will move everything to the STATIC_ROOT folder for deployment.
这篇关于Django STATIC_URL只能在没有斜杠的情况下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!