可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
Read this carefully:
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
Is django.contrib.staticfiles
in your INSTALLED_APPS
in settings.py
?
Is DEBUG=False
? If so, you need to call runserver
with the --insecure
parameter:
python manage.py runserver --insecure
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.
回答2:
With Django 1.11.x+, you have to configure static files in settings.py
as,
STATIC_URL = '/static/' # the path in url
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
and use it with static template tag,
{% load static %}
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
回答3:
Another simple thing to try is to stop, and then restart the server e.g.
$ python manage.py runserver
I looked into the other answers, but restarting the server worked for me.
回答4:
Are these missing from your settings.py
? I am pasting one of my project's settings:
TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages")
Also, this is what I have in my urls.py
:
urlpatterns += patterns('', (
r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': 'static'}
))
回答5:
Add the following code to your settings.py
:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
After that, create the static folder at the root directory of your project.
To load the static files on templates use:
{% load static %}
<img src="{% static "images/index.jpeg" %}" alt="My image"/>
回答6:
These steps work for me, just see Load Static Files (CSS, JS, & Images) in Django
I use Django 1.10.
- create a folder
static
on the same level of settings.py
, my settings.py
's path is ~/djcode/mysite/mysite/settings.py
, so this dir is ~/djcode/mysite/mysite/static/
;
- create two folders
static_dirs
and static_root
in static
, that's ~/djcode/mysite/mysite/static/static_dirs/
and ~/djcode/mysite/mysite/static/static_root/
;
write settings.py
like this:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'mysite', 'static', 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'mysite', 'static', 'static_dirs'),
)
do this command $ python manage.py collectstatic
in shell;
create a folder css
in static_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!
回答7:
added
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, "static"), )
and removed STATIC_ROOT
from settings.py
, It worked for me
回答8:
Add this "django.core.context_processors.static",
context processor in your settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.static",
)
回答9:
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.
回答10:
Have you added into your templates:
{% load staticfiles %}
This loads what's needed, but for some reason I have experienced that sometimes work without this... ???
回答11:
DEBUG = True
in my local settings did it for me.
回答12:
See if your main application (where the static directory is located) is included in your INSTALLED_APPS.
Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.
回答13:
I had to use
STATICFILES_DIRS = ( '/home/USERNAME/webapps/django/PROJECT/static/', )
That helped me.
回答14:
I tried this model and it worked.
Changes in settings as per the django project created with shell
"django-admin.py startproject xxx"# here xxx is my app name
modify the folder as below structure loading our static files to run on server
Structure of xxx is:
> .
> |-- manage.py
> |-- templates
> | `-- home.html
> `-- test_project
> |-- __init__.py
> |-- settings.py
> |-- static
> | |-- images
> | | `-- 01.jpg
> | |-- style.css
> |-- urls.py
> `-- wsgi.py
- modifications in Settings.py
import os
INSTALLED_APPS = ( 'xxx',# my app is to be load into it)
STATIC_ROOT = ''
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(__file__)
TEMPLATE_DIRS = ( os.path.join(PROJECT_DIR, '../templates'),)#include this
- modifications in urls.py
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
class DirectTemplateView(TemplateView):
extra_context = None
def get_context_data(self, **kwargs):
context = super(self.__class__, self).get_context_data(**kwargs)
if self.extra_context is not None:
for key, value in self.extra_context.items():
if callable(value):
context[key] = value()
else:
context[key] = value
return context
urlpatterns = patterns('',
url(r'^$', DirectTemplateView.as_view(template_name="home.html")), )
- home.html
<html>
<head>
<link href="{{STATIC_URL}}style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>This is home for some_app</h1>
<img src="{{STATIC_URL}}/images/01.jpg" width=150px;height=150px; alt="Smiley ">
</body>
</html>