Django - 无法获取要加载的静态CSS文件 [英] Django -- Can't get static CSS files to load

查看:384
本文介绍了Django - 无法获取要加载的静态CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地机器(Mac OS X)上运行Django的开发服务器( runserver ),无法获取要加载的CSS文件。

I'm running Django's development server (runserver) on my local machine (Mac OS X) and cannot get the CSS files to load.

以下是settings.py中的相关条目:

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',
)

在我的views.py中,我要求上下文:

In my views.py I'm requesting the context:

return render_to_response("cal/main.html",dict(entries=entries),context_instance=RequestContext(request))

在我的模板中, {{STATIC_URL}} 正确呈现:

And in my template the {{ STATIC_URL }} renders correctly:

<link type="text/css" href="{{ STATIC_URL }}css/main.css" />

变成:

<link type="text/css" href="/static/css/main.css"/>

文件实际位于哪个位置。我还运行 collectstatic 以确保所有文件都已收集。

Which is where the file is actually located. I also ran collectstatic to make sure all the files were collected.

我的网址中还有以下几行.py:

I also have the following lines in my urls.py:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()

我是Django的新手,所以我可能会错过一些简单的东西 - 会感谢任何帮助。 / p>

I'm new to Django so am probably missing something simple -- would appreciate any help.

推荐答案

仔细阅读:
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

是'你的INSTALLED_APPS中有django.contrib.staticfiles?

Is 'django.contrib.staticfiles' in your INSTALLED_APPS?

DEBUG = False?如果是这样,您需要使用--insecure参数调用runserver。

Is DEBUG=False? If so, you need to call runserver with the --insecure parameter.

'collectstatic'与通过开发服务器提供文件无关。它用于在一个位置(STATIC_ROOT)中收集静态文件,以便您的Web服务器找到它们。事实上,将STATIC_ROOT设置为STATICFILES_DIRS中的一个路径,运行collectstatic是一个坏主意。您应该仔细检查以确保您的CSS文件现在仍然存在。

'collectstatic' has no bearing on serving files via the development server. It is for collecting the static files in one location (STATIC_ROOT) for your web server to find them. In fact, running collectstatic with your STATIC_ROOT set to a path in STATICFILES_DIRS is a bad idea. You should double-check to make sure your CSS files even exist now.

这篇关于Django - 无法获取要加载的静态CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆