Django——无法加载静态 CSS 文件
我正在本地计算机 (Mac OS X) 上运行 Django 的开发服务器 (runserver
),但无法加载 CSS 文件。
以下是settings.py中的相关条目:
STATIC_ROOT = '/Users/username/Projects/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/Users/thaymore/Projects/mysite/cal/static',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
INSTALLED_APPS = (
# other apps ...
'django.contrib.staticfiles',
)
在我的views.py中,我请求上下文:
return render_to_response("cal/main.html",dict(entries=entries),context_instance=RequestContext(request))
在我的模板中, {{ STATIC_URL }}
正确呈现:
<link type="text/css" href="{{ STATIC_URL }}css/main.css" />
变成:
<link type="text/css" href="/static/css/main.css"/>
文件所在的位置实际上位于。我还运行了 collectstatic
以确保收集所有文件。
我的 urls.py 中也有以下几行:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
我是 Django 新手,所以可能缺少一些简单的东西 - 希望得到任何帮助。
I'm running Django's development server (runserver
) on my local machine (Mac OS X) and cannot get the CSS files to load.
Here are the relevant entries in settings.py:
STATIC_ROOT = '/Users/username/Projects/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/Users/thaymore/Projects/mysite/cal/static',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
INSTALLED_APPS = (
# other apps ...
'django.contrib.staticfiles',
)
In my views.py I'm requesting the context:
return render_to_response("cal/main.html",dict(entries=entries),context_instance=RequestContext(request))
And in my template the {{ STATIC_URL }}
renders correctly:
<link type="text/css" href="{{ STATIC_URL }}css/main.css" />
Turns into:
<link type="text/css" href="/static/css/main.css"/>
Which is where the file is actually located. I also ran collectstatic
to make sure all the files were collected.
I also have the following lines in my urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
I'm new to Django so am probably missing something simple -- would appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
仔细阅读以下内容:
https://docs.djangoproject.com/en/dev/ref/contrib/ staticfiles/
django.contrib.staticfiles
是否位于settings.py
中的INSTALLED_APPS
中?是
DEBUG=False
吗?如果是这样,您需要使用--insecure
参数调用runserver
:collectstatic
与通过开发服务器提供文件无关。它用于将静态文件收集到一个位置STATIC_ROOT
,以便您的网络服务器找到它们。事实上,在将STATIC_ROOT
设置为STATICFILES_DIRS
中的路径的情况下运行collectstatic
是一个坏主意。您应该仔细检查以确保您的 CSS 文件现在存在。Read this carefully:
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
Is
django.contrib.staticfiles
in yourINSTALLED_APPS
insettings.py
?Is
DEBUG=False
? If so, you need to callrunserver
with the--insecure
parameter:collectstatic
has no bearing on serving files via the development server. It is for collecting the static files in one locationSTATIC_ROOT
for your web server to find them. In fact, runningcollectstatic
with yourSTATIC_ROOT
set to a path inSTATICFILES_DIRS
is a bad idea. You should double-check to make sure your CSS files even exist now.对于最新版本的 Django,您必须在
settings.py
中配置静态文件,并将其与静态模板标签一起使用,
For recent releases of Django, You have to configure static files in
settings.py
as,and use it with static template tag,
另一个简单的尝试是停止,然后重新启动服务器,例如
我查看了其他答案,但重新启动服务器对我有用。
Another simple thing to try is to stop, and then restart the server e.g.
I looked into the other answers, but restarting the server worked for me.
您的
settings.py
中是否缺少这些内容?我正在粘贴我的项目设置之一:另外,这是我的
urls.py
中的内容:Are these missing from your
settings.py
? I am pasting one of my project's settings:Also, this is what I have in my
urls.py
:从
settings.py
添加和删除
STATIC_ROOT
,它对我有用added
and removed
STATIC_ROOT
fromsettings.py
, It worked for me将以下代码添加到您的
settings.py
中:之后,在项目的根目录中创建 static 文件夹。
要在模板上加载静态文件,请使用:
Add the following code to your
settings.py
:After that, create the static folder at the root directory of your project.
To load the static files on templates use:
我的本地设置中的
DEBUG = True
为我做到了。DEBUG = True
in my local settings did it for me.这些步骤对我有用,只需参阅 在 Django 中加载静态文件(CSS、JS 和图像)< /a>
我使用 Django 1.10。
settings.py
同级创建一个文件夹static
,我的settings.py
的路径是~/djcode/mysite /mysite/settings.py
,所以这个目录是~/djcode/mysite/mysite/static/
;static
中创建两个文件夹static_dirs
和static_root
,即~/djcode/mysite/mysite/static/static_dirs/
和~/djcode/mysite/mysite/static/static_root/
;像这样编写
settings.py
:在shell中执行此命令
$ python manage.pycollectstatic
;在
static_dirs
中创建一个文件夹css
并放入您自己的.css
文件中,您的css文件路径为~/ djcode/mysite/mysite/static/static_dirs/css/my_style.css
;更改
.html
文件中的标记:
,
最后这个链接的路径是
http://192.168.1.100:8023/static/ css/my_style.css
宾果!
These steps work for me, just see Load Static Files (CSS, JS, & Images) in Django
I use Django 1.10.
static
on the same level ofsettings.py
, mysettings.py
's path is~/djcode/mysite/mysite/settings.py
, so this dir is~/djcode/mysite/mysite/static/
;static_dirs
andstatic_root
instatic
, that's~/djcode/mysite/mysite/static/static_dirs/
and~/djcode/mysite/mysite/static/static_root/
;write
settings.py
like this:do this command
$ python manage.py collectstatic
in shell;create a folder
css
instatic_dirs
and put into your own.css
file, your css file' path is~/djcode/mysite/mysite/static/static_dirs/css/my_style.css
;change
<link>
tag in.html
file:<link rel="stylesheet" type="text/css" href="{% static 'css/my_style.css' %}">
,Finally this link's path is
http://192.168.1.100:8023/static/css/my_style.css
Bingo!
您在 STATICFILES_DIRS 和 STATIC_ROOT 中有相同的路径,我遇到了同样的问题,下面是例外 -
ImproperlyConfigured: The STATICFILES_DIRS 设置不应包含 STATIC_ROOT 设置
对于本地,您不需要 STATICFILES_DIRS,因为无论如何您不需要运行收集静态。一旦你评论它,它应该可以正常工作。
You had same path in STATICFILES_DIRS AND STATIC_ROOT, I ran into the same issue and below was the exception -
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
For local you don't need STATICFILES_DIRS, as anyway you don't need to run collectstatic. Once you comment it, it should work fine.
您是否在模板中添加了:
这会加载所需的内容,但由于某种原因,我经历过有时没有这个就可以工作...???
Have you added into your templates:
This loads what's needed, but for some reason I have experienced that sometimes work without this... ???
我必须使用
这对我有帮助。
I had to use
That helped me.
查看您的主应用程序(静态目录所在的位置)是否包含在您的 INSTALLED_APPS 中。
See if your main application (where the static directory is located) is included in your INSTALLED_APPS.
我尝试了这个模型并且成功了。
根据使用 shell 创建的 django 项目更改设置,
修改文件夹如下结构加载我们的静态文件以在服务器上运行
xxx 的结构是:
- Settings.py 中的修改
- urls.py 中的修改
- home.html
I tried this model and it worked.
Changes in settings as per the django project created with shell
modify the folder as below structure loading our static files to run on server
Structure of xxx is:
- modifications in Settings.py
- modifications in urls.py
- home.html
在您的 settings.py 中添加此
"django.core.context_processors.static",
上下文处理器)
Add this
"django.core.context_processors.static",
context processor in your settings.py)
您可以根据您是在本地主机上运行还是在服务器上运行来设置 STATIC_ROOT 。要确定这一点,请参阅此
您可以将 STATIC_ROOT 配置重写为:
You can just set STATIC_ROOT depending on whether you are running on your localhost or on your server. To identify that, refer to this post.
And you can rewrite you STATIC_ROOT configuration as:
如果设置 DEBUG=FALSE
您需要
在 urls.py 文件中执行以下步骤:
添加这一行
If you set DEBUG=FALSE
you need to do follow steps
In your urls.py file:
add this line
我有同样的问题(ununtu 16.04 服务器)。
这对我有帮助
I have the same issue (ununtu 16.04 server).
This helped me
在settings.py中添加以下内容
add following in settings.py
在 Django 应用程序中运行静态文件需要注意的两个最基础的点是 - 在
settings.py
文件中声明静态文件路径另一个重要的参数是您使用 static 关键字的网页,您需要加载静态文件。
Two most Basis points to be noted for running Static files in Django Application are - Declare static file path in your
settings.py
fileAnother important parameter is the web page in which you are using static keyword, you need to load the static files.
转到您的 HTML 页面加载静态
现在我犯的唯一错误是这个
我的代码:
更新:
你说得对
Go to your HTML page load static by
Now only mistake I've made was this
My code:
Updated:
You get it right
我遇到了同样的问题,请检查您的 settings.py 并确保 STATIC_URL = '/static/'
就我而言,第一个/开头丢失,这导致所有静态文件无法工作
I had same issue check your settings.py and make sure STATIC_URL = '/static/'
in my case first / at the beginning was missing and that was causing all static files not to work