I want to see how my app works in production and i changed settings in my local machine, so i used below settings in settings.py
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
STATIC_URL = '/assets/'
STATIC_ROOT = os.path.join(BASE_DIR, 'assets/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I used python manage.py collectstatic which copied all the files from folder static to assets
when i set Debug = True files served from assets with 200, one example (http://localhost:8000/assets/js/jquery-2.2.4.js)
but when i set Debug = False the static files are not found with 404 error.
When you turn on debug, you server should take care about static files, anyway if you want continue using in debug = True run it in mode insecure like python manage.py runserver --insecure
Related
When i run django on uwsgi with;
uwsgi --http :8081 --module proj1.wsgi
It gives below error when i open start page from browser:
Not Found: /accounts/login/static/css/style.css
But my settings.py is :
STATIC_URL = 'static/'
STATIC_ROOT = '/home/proj1/static/'
STATICFILES_DIRS = (
'/home/proj1/staticorj/static/',
)
When i make collectstatic, it copies files to static root without problem.
I dont understand why does it look for /accounts/login.
It should look in /home/proj1 directory for static dir.
So the browser opens the page but without serving static files.
Statics operation should be like this.
First configure settings.py like below :
STATIC_URL = 'static/'
STATIC_ROOT = '/home/proj1/static/'
STATICFILES_DIRS = (
'/home/proj1/staticorj/static/',
)
Put statics files that you need in /home/proj1/staticorj/static/
Then run :
python manage.py collectstatic
This will carry /home/proj1/staticorj/static/ files to /home/proj1/static/ directory, and now django will use /home/proj1/static/ directory for static source.
If you will not make any statics changes set as below with commenting.
STATIC_URL = '/static/'
#STATIC_ROOT = '/home/proj1/static/'
#STATICFILES_DIRS = (
# '/home/proj1/staticorj/static/',
#)
And run your server. Better use uwsgi and nginx like apps to serv. In production files will be served. Also use new version browser.
I have a working app and have downloaded the relevant django files locally (via git) and am trying to run the app locally. However, static files are not loading.
I receive the following console error when accessing, say, the home page (http://localhost:8000/home/) of the app:
GET http://localhost:8000/static/imported_JS/jquery/jquery.min.js net::ERR_ABORTED
or this error:
http://localhost:8000/static/globe.png 404 (NOT FOUND)
In my settings.py file, I can confirm:
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)),'..')) # i.e. location of settings.py file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
I can also confirm there is indeed a static_cdn/ file directory and it does contain the relevant static files.
The live dev and prod versions of the app work (which are hosted on separate web servers) but, for whatever reason, when running the app locally, static files are not served.
Any ideas on how to have the localhost server static files? Most of the answers on SO regarding this type of question relate to mistakes with setting up STATIC_URL vs. STATIC_ROOT but these appear to be correct (at least on dev and prod).
Let me know if you require more info. Thank you.
UPDATE
Well, I struggled with this problem for an hour or so, and 5mins after posting this SO question, I think I found a solution.
Changing from this:
STATICFILES_DIRS = []
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
to this made the difference:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_cdn/') # add STATIC_ROOT to DIRS
]
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
I'm not sure why this works but I'll indicate your response as Answer if you explain why. Thanks.
I have a Django 1.9.6 site deployed to Heroku. When DEBUG=False I was getting a server error (500). The logs contained no useful information, so I tried running it with DEBUG=True. Now it works fine. I think the issue may be tied to my scss file processing, which really confuses me and I was struggling with. I recently--among other things--added COMPRESS_OFFLINE = True to my settings files, and commenting that out seems to alleviate the problem (although then my scss files don't work).
Some of my static settings.py. Let me know if you need more--so much of this is a mystery to me. I was trying to follow this as best as I could.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# other finders..
'compressor.finders.CompressorFinder',
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
in urls.py:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
url(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT
}),
]
urlpatterns += staticfiles_urlpatterns()
EDIT:
I've gotten logging to work, and I've confirmed that it's a compress error. I'm getting the error message:
Internal Server Error: /
OfflineGenerationError at /
You have offline compression enabled but key "171c3b7763dbc51a465d996f7d920cf5" is missing from offline manifest. You may need to run "python manage.py compress".
which is the same thing I've gotten locally, except running the suggested command solved it. Running heroku run python manage.py compress doesn't have an effect (no errors running it, though)
The manifest generated by compress was stored in my .gitignore and therefore the one on production was stale. Adding it to the git repository fixed everything.
First off set value for ALLOW_HOSTS, this can't be blank when debug is off.
ALLOWED_HOSTS = ['.mydomain.com', '.2nddomain.com']
Because you use compress plugins:
SET
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
# this where the collectstatic and compress result output
# point your static alias to here
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# in your production env: activate ur virtual environment then run the compress statics command
python manage.py compress
python manage.py collectstatic
When Debug is off all exceptions is suppressed for security reason, set admin email in the setting file to let django email all un-caught exception
SERVER_EMAIL = 'ur#from-email-address.com'
ADMINS = (
('Exceptions Email', 'destination#email.com'),
)
Add this to your settings.py inside the loggers section and it should give you more information (this is what helped point me into solving the same problem).
"django.request": {
"handlers": ["console"],
"level": "ERROR",
"propagate": True
}
For what it's worth, here are my similar settings.py settings:
MEDIA_URL = "http://%s.s3.amazonaws.com/" % (AWS_STORAGE_BUCKET_NAME)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = os.getenv("DJANGO_STATIC_HOST", "") + "/static/"
if DEBUG:
STATIC_URL = "/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Note: I have no MEDIA_ROOT or STATICFILES_FINDERS and I'm also using Whitenoise with CloudFront for my static file handling
Today I tried to share a website with 'PythonAnywhere'. I have encountered the same problem and have fixed the problem with 'Allowed_Host'.
https://docs.djangoproject.com/en/1.10/ref/settings/#allowed-hosts
settings.py
ALLOWED_HOSTS = ['*']
I'm trying to deploy my Django application to the web, but I get the following error:
You're using the staticfiles app without having set the STATIC_ROOT
setting to a filesystem path
However, I did in my production.py:
from django.conf import settings
DEBUG = False
TEMPLATE_DEBUG = True
DATABASES = settings.DATABASES
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# Update database configuration with $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
What is the production.py file? How do you import your settings?
Depending on how you got this error (serving django through a wsgi server or on the command line), check for manage.py or wsgi.py to see what is the name of the default settings file.
If you want to manuallly set the settings to use, use something like this:
./manage.py --settings=production
Where production is any python module.
Moreover, your settings file should not import anything django related. If you want to split your settings for different environments, use something like this.
A file settings/base.py
# All settings common to all environments
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
Files like settings/local.py, settings/production.py…
# Production settings
from settings.base import *
DEBUG = False
DATABASES = …
If you are using Django 2.2 or greater, your settings file already has a line similar to this:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Therefore you can easily set static like so:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Set the STATIC_ROOT setting to the directory from which you’d like to serve static files, for example:
STATIC_ROOT = "/var/www/example.com/static/"
The settings you are using are for development. Check the Django docs for more information here
Django settings for static assets can be a bit difficult to configure and debug. However, if you just add the following settings to your settings.py, everything should work exactly as expected:
goto "settings.py" add following code
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
See a full version of our example settings.py on GitHub.
now create static folder in root directory, and a random file inside
it.
Django won’t automatically create the target directory (STATIC_ROOT) that collectstatic uses, if it isn’t available. You may need to create this directory in your codebase, so it will be available when collectstatic is run. Git does not support empty file directories, so you will have to create a file inside that directory as well.
for more refer: https://devcenter.heroku.com/articles/django-assets
I have two directories called media and static.
When I have DEBUG = True everything works but if I change it to DEBUG = False the problems arises.
I've read that my server will handle static files in production. I have understood it as
In development it's fine to have all my static files in my directory static but when I go in production I need to move all my static files to another directory (it could be static_www?) and before starting the server I run python manage.py collectstatic. This command will move all the files from static_www to static and everything works.
But why do I need to have two separate directories with same content? How do the server know that my files 'has been collected' through collectstatic (it's just files in a folder so how can it know the difference)? I guess collectstatic is primarily used when you have static files in multiple directories and want to collect them all before you go into production.
The variables in my settings.py looks like:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_www"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
and it moves from static_www to static so neither in production nor development my directory static_www is not used.
Maybe all these settings are meant for websites which has another server only for handling static files?
STATIC_ROOT is where the virtual directory for STATIC_URL will point.
Example:
STATIC_ROOT = '/var/www/example.com/static/'
STATIC_URL = 'http://www.example.com/static/'
collectstatic will find all static files in STATICFILES_DIR and copy to STATIC_ROOT.
A request to http://www.example.com/static/file.jpg will be handled by Django and /var/www/example.com/static/file.jpg will be served.
You can set your static files to be even in another site, using something like:
STATIC_ROOT = '/var/www/static.example.com/static/'
STATIC_URL = 'http://static.example.com/static/'
In this way, static.example.com can be a simple server which will serve files without they be handled by Django, increasing the site performance. Or you can upload to a CDN like Amazon S3 (and CloudFront), etc.