[英]Static Files With Django Development Server 1.7 Slightly Different Directory Settings
This is my website directory: 这是我的网站目录:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\js
\css
\media
\templates
base.html
What I have added to my settings.py is: 我添加到settings.py中的是:
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "templates"),
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static"),
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media"),
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static"),
),
and to my urls.py: 和我的urls.py:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and hey, it does'nt work. 嘿,它不起作用。 The way I refer to them in the base.html is: 我在base.html中引用它们的方式是:
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
Any ideas? 有任何想法吗?
Thank you. 谢谢。 Hasan 哈桑
Running the settings that you've pasted gave me an error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
. 运行您粘贴的设置会给我一个错误的ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
。 Commenting-out os.path.join(os.path.dirname(BASE_DIR), "static"),
in the settings removed the error and the template correctly found the CSS file without the URLs.py modifications. 注释掉os.path.join(os.path.dirname(BASE_DIR), "static"),
在设置中删除了错误,并且模板正确地找到了CSS文件,而没有对URLs.py进行修改。
First of all as @r---------k mentioned, I should not have had trailing commas except for the tuples. 首先,如@r --------- k所述,除元组外,我不应该有逗号结尾。 So the settings.py should look like: 所以settings.py应该看起来像:
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
And the second thing is that, as you may notice, STATIC_ROOT and STATICFILES_DIRS cannot possibly have the same value. 第二件事是,您可能已经注意到,STATIC_ROOT和STATICFILES_DIRS不可能具有相同的值。 I added another static folder inside the static folder and put my js and css folders inside of it: 我在静态文件夹中添加了另一个静态文件夹,并将我的js和css文件夹放入其中:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
The directory looks like: 目录如下:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\static
\js
\css
\media
\templates
base.html
Finally, you should have the urlpatterns in your urls.py: 最后,您应该在urls.py中包含urlpatterns:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This seems to work in current django (2.2) in your urls.py
: 这似乎在urls.py
中的当前django(2.2)中有效:
from django.conf import settings
from django.conf.urls.static import serve
from django.urls import include, path
urlpatterns += [
path(settings.STATIC_URL[1:], serve, {'document_root': settings.STATIC_ROOT })
]
Remember to run ./manage.py collectstatic
to collect the static files to settings.STATIC_ROOT
请记住运行./manage.py collectstatic
将静态文件收集到settings.STATIC_ROOT
./manage.py collectstatic
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.