93π
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.
37π
For recent releases of Django, 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' %}">
- [Django]-Limit number of characters with Django Template filter
- [Django]-Django β what is the difference between render(), render_to_response() and direct_to_template()?
- [Django]-How to reset migrations in Django 1.7
8π
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.
- [Django]-Detect django testing mode
- [Django]-How to override css in Django Admin?
- [Django]-How to set and get cookies in Django?
3π
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'}
))
- [Django]-MySQL vs PostgreSQL? Which should I choose for my Django project?
- [Django]-Django 2, python 3.4 cannot decode urlsafe_base64_decode(uidb64)
- [Django]-Django : DRF Token based Authentication VS JSON Web Token
3π
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
- [Django]-Fastest way to get the first object from a queryset in django?
- [Django]-Unable log in to the django admin page with a valid username and password
- [Django]-Django staticfiles not found on Heroku (with whitenoise)
3π
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"/>
- [Django]-Class has no objects member
- [Django]-How to do math in a Django template?
- [Django]-Django β How to do tuple unpacking in a template 'for' loop
- [Django]-Django aggregate or annotate
- [Django]-Scoped_session(sessionmaker()) or plain sessionmaker() in sqlalchemy?
- [Django]-How to go from django image field to PIL image and back?
2π
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 ofsettings.py
, mysettings.py
βs path is~/djcode/mysite/mysite/settings.py
, so this dir is~/djcode/mysite/mysite/static/
; - create two folders
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:# 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
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!
- [Django]-Malformed Packet: Django admin nested form can't submit, connection was reset
- [Django]-'RelatedManager' object has no attribute
- [Django]-Django values_list vs values
0π
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.
- [Django]-How to force Django models to be released from memory
- [Django]-Django create userprofile if does not exist
- [Django]-Where should utility functions live in Django?
0π
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β¦ ???
- [Django]-How to get URL of current page, including parameters, in a template?
- [Django]-Django model object with foreign key creation
- [Django]-Django project models.py versus app models.py
0π
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>
- [Django]-Django set field value after a form is initialized
- [Django]-Django @login_required decorator for a superuser
- [Django]-Django abstract models versus regular inheritance
0π
I had to use
STATICFILES_DIRS = ( '/home/USERNAME/webapps/django/PROJECT/static/', )
That helped me.
- [Django]-How to do SELECT MAX in Django?
- [Django]-Best practices for getting the most testing coverage with Django/Python?
- [Django]-How to get Request.User in Django-Rest-Framework serializer?
0π
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.
- [Django]-How to write django test meant to fail?
- [Django]-Django.contrib.auth.logout in Django
- [Django]-Get user profile in django
0π
Add this "django.core.context_processors.static",
context processor in your settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.static",
)
- [Django]-How to server HTTP/2 Protocol with django
- [Django]-How can I modify Procfile to run Gunicorn process in a non-standard folder on Heroku?
- [Django]-Django "xxxxxx Object" display customization in admin action sidebar
0π
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:
import sys
if 'runserver' in sys.argv:
STATIC_ROOT = ''
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
- [Django]-How can I build multiple submit buttons django form?
- [Django]-Is there a naming convention for Django apps
- [Django]-Django β [Errno 111] Connection refused
0π
If you set DEBUG=FALSE
you need to do follow steps
In your urls.py file:
add this line
from django.views.static import serve
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
- [Django]-Django: Calculate the Sum of the column values through query
- [Django]-Access web server on VirtualBox/Vagrant machine from host browser?
- [Django]-Custom QuerySet and Manager without breaking DRY?
0π
I have the same issue (ununtu 16.04 server).
This helped me
python manage.py collectstatic βnoinput
- [Django]-Easiest way to rename a model using Django/South?
- [Django]-Can you change a field label in the Django Admin application?
- [Django]-Django models ForeignKey on_delete attribute: full meaning?
0π
add following in settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
- [Django]-"Failed building wheel for psycopg2" β MacOSX using virtualenv and pip
- [Django]-Django get list of models in application
- [Django]-Accessing dictionary by key in Django template
0π
Two most Basis points to be noted for running Static files in Django Application are β Declare static file path in your settings.py
file
STATIC_URL = '/static/'
Another important parameter is the web page in which you are using static keyword, you need to load the static files.
{% load static %}
- [Django]-Write only, read only fields in django rest framework
- [Django]-Django Model() vs Model.objects.create()
- [Django]-How to make the foreign key field optional in Django model?
0π
Go to your HTML page load static by
{% load static %}
Now only mistake Iβve made was this
My code:
<img src="**{% static** "images/index.jpeg" %}" alt="My image">
Updated:
<img src=**"{% static 'images/index.jpeg' %}' alt="My image"**>
You get it right
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
- [Django]-Itertools.groupby in a django template
- [Django]-Adding new custom permissions in Django
0π
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
- [Django]-How do I install an old version of Django on virtualenv?
- [Django]-Django Rest Framework pagination extremely slow count
- [Django]-How do I set up Jupyter/IPython Notebook for Django?