10π
With DEBUG=False
, what originals use to work does not work for me anymore.
However a fix by enabling whitenoise
on MIDDLEWARE
in settings.py
solved it. Best to be just below SecurityMiddleware
.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # add this line
#Other middleware...
]
β`
According to the docs, it actually needs to be enabled in the first place.
7π
I got it. I needed to add
python manage.py collectstatic --noinput;
in my Procfile. Heroku doc said that collecticstatic
is automatically triggered.
https://devcenter.heroku.com/articles/django-assets
Thanks
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-Django Multiple Authentication Backend for one project
- [Django]-How to get username from Django Rest Framework JWT token
5π
For me following worked.
settings.py
DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') #this is not used
# Add static folder to STATIC_DIRS
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
urls.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Note
This helper function works only in debug mode and only if the given
prefix is local (e.g. /static/) and not a URL (e.g.
http://static.example.com/).Also this helper function only serves the actual STATIC_ROOT folder;
it doesnβt perform static files discovery like
django.contrib.staticfiles.
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-Django Admin Form for Many to many relationship
3π
The problem is that the Python application in Heroku uses the built-in web server and does not serve static files.
You can use the app of the whitenoise, this working solution is 100%.
Suppose you have already generated static files, for example:
$ python manage.py collectstatic
Next you need to do this:
1) $ pip install whitenoise
2) add string βwhitenoise==3.3.0β in your requirements.txt
3) add code in settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
4) add this code in app/wsgi.py
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
- [Django]-How can I access environment variables directly in a Django template?
- [Django]-Choose test database?
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
2π
for BASE_DIR you need to a double dirname if your settings are not in the root but in /projectname/ folder :
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# for /static/root/favicon.ico
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'staticfiles', 'root')
template.html
{% load staticfiles %}
<link rel="stylesheet" href="{% static "app/css/font.css" %}">
app tree for this example:
annuaire
|-- /annuaire
|-- -- /settings.py
|-- /app
|-- /static/app/css/font.css
- [Django]-How to serve media files on Django production environment?
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-Removing 'Sites' from Django admin page
2π
In addition to above answers, it can also be that you have not specified a correct STATIC_ROOT as described in https://docs.djangoproject.com/en/2.0/howto/static-files/#deployment
For me, the solution was adding this to the end of my production settings.py
STATIC_ROOT = "/app/static/"
To know where your static folder is in your heroku run this
heroku run python manage.py collectstatic
Then you will see the path being shown there.
- [Django]-Django Admin Form for Many to many relationship
- [Django]-Django: Use of DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT in settings.py?
- [Django]-Altering one query parameter in a url (Django)
1π
I struggled a couple of hours until I finally figured out the problem. The main problem in my opinion is that in the Heroku official documentation they use the Old-style middleware that uses MIDDLEWARE_CLASSES
which is deprecated, instead of the new MIDDLEWARE
setting.
In whitenoise version 4+, the WSGI integration option for Django (which involved editing wsgi.py) has been removed. Instead, you should add WhiteNoise to your middleware list in settings.py and remove any reference to WhiteNoise from wsgi.py. (from the docs)
The following configuration worked like a charm:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
# the next line of code is the one that solved my problems
'whitenoise.middleware.WhiteNoiseMiddleware',
]
Pay attention to the following Note, also from the docs.
You might find other third-party middleware that suggests it should be given highest priority at the top of the middleware list. Unless you understand exactly what is happening you should ignore this advice and always place WhiteNoiseMiddleware above other middleware.
- [Django]-Heroku, postgreSQL, django, comments, tastypie: No operator matches the given name and argument type(s). You might need to add explicit type casts
- [Django]-Django 2 β How to register a user using email confirmation and CBVs?
- [Django]-Missing Table When Running Django Unittest with Sqlite3
1π
I have had the same issue. The simplest way to find the problem is to use
heroku run ls staticfiles/images
if images is in the directory where your files should be. This will give you a list of all the files in that directory.
As I found out it was a case issue in the file extension. The file had an extension .JPG
and I referenced it on the template with an extension .jpg
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
- [Django]-Django β getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}
- [Django]-Heroku, postgreSQL, django, comments, tastypie: No operator matches the given name and argument type(s). You might need to add explicit type casts
1π
$ pip install whitenoise
add whitenoise in middleware.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
also need to add STATIC_ROOT in setting.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
and add media_root in urls.py
from django.conf import settings
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and then you can run collectstatic command locally or on server python manage.py collectstatic
and it will work properly. you can run this command locally collectstatic and it will work on server.
- [Django]-Missing Table When Running Django Unittest with Sqlite3
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-Django JSONField inside ArrayField
0π
this is worked for me
my requirements.txt is
whitenoise==6.2.0
static3==0.7.0
webdav4==0.9.7
Django==3.2
# others
I delete STATIC_ROOT
in settings.py
and add var STATICFILES_DIRS
, my settings.py
:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
"whitenoise.middleware.WhiteNoiseMiddleware",
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_URL = 'static/'
if you set heroku config:set DISABLE_COLLECTSTATIC=1
, now you should run properly
if you still want use COLLECTSTATIC function
add it manually in Procfile
file as:
web: python manage.py collectstatic --no-input; gunicorn my_blog.wsgi --log-file - --log-level debug
my my_blog.wsgi
file is(itβs configed by following a post which cannot be found now):
import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_blog.settings')
application = Cling(get_wsgi_application())
- [Django]-How to get username from Django Rest Framework JWT token
- [Django]-How to serve media files on Django production environment?
- [Django]-Filtering dropdown values in django admin