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 = ['*']
Related
My problem is that when I set the DEBUG in settings.py file to False and add the localhost to ALLOWED_HOSTS, then my admin page gets empty of css.
I want to solve the issue and know why it happens
I have tried running the collectstatic command I don't get any error however, the problem still exists
`DEBUG = False
ALLOWED_HOSTS = [
'localhost',
'127.0.0.1'
]`
place this code in your settings.py, then you have the collectstatic
update Try this way for your project.:
urlpatterns = patterns('',
....urls......
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
in your settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
REPOSITORY_ROOT = os.path.dirname(BASE_DIR)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
ROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') # specify static root
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(REPOSITORY_ROOT, 'media/')
In your shell
python manage.py collectstatic
This worked fine everytime used to do django websites but this time it is giving me an error.
Settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'portfolio/static/')
]
STATIC_ROOT = os.path.join(BASE_DIR , 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
I have a profile.jpg in my directory Portfolio-Project/Portfolio/static/profile.jpg. It should collectstatic from here and paste the staticfiles in Portfolio-project/static as mentioned in my code. but it is giving ,me some error.
Error After using the command "Python manage.py collectstatic"
django.core.exceptions.SuspiciousFileOperation: The joined path
(C:\Users\Kiran\Desktop\portfolio-project\portfolio\static\Profile.jpg) is
located outside of the base path component
(C:\Users\Kiran\Desktop\portfolio- project\portfolio\static\)
Please Help.
Thanks
In your line:
os.path.join(BASE_DIR, 'portfolio/static/')
Delete the last slash:
os.path.join(BASE_DIR, 'portfolio/static')
Anyway, this is the ideal:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
I recently faced this error but it‘s actually simple:
Chances are you downloaded a Template, here is the solution:
First you have to check your css file for stuff like relative links
For example your CSS might be referencing a file outside your django project itself.
e.g. backround:url('...\image\Profile.jpg') this actually worked for me
Note:
The key fact is just to check your CSS or (maybe js) file first if it‘s a referencing file that you‘re not using or file that is referring to something that is not in your django project directory.
So I upload my site to digitalocean and when I went to the admin page the CSS was not showing
I visit all these sites but nothing seems to work
Django doc-static files
Pythonanywhere-DjangoStaticFiles
StackOverflow -why my django admin site does not have the css style
I set up my STATIC_ROOT and STATIC_URL, and then I ran
python manage.py collectstatic
And here is my seting
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/home/django/django_project/django_project/static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
You are specifying an Absolute Path for your join.
os.path.join(BASE_DIR, arg2) means join the current directory that is being executed and append the second argument.
add these lines into your settings.py file
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
I'm working with an existing (and previously functional) Django site. We recently upgraded from Django 1.8.13 to 1.10 and our WSGI is Gunicorn. It works fine when hosted from my development machine, but when deployed, all static resources (on the admin and the main site) yield 404's with the message, Directory indexes are not allowed here.
Our settings.py contains the following:
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
DEBUG = True
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_resources')
The directory structure looks like this:
/my-project-name
/my-project-name
server.py
settings.py
urls.py
wsgi.py
...
/static
/static_resources
manage.py
Django does not serve static files in production mode (DEBUG=False). On a production deployment that's the job of the webserver. To resolve the problem:
run python manage.py collectstatic
in your web server configuration point the /static folder to the static folder of Django
Don't just turn DEBUG on, it would be dangerous!
Try to change os.path.join(PROJECT_DIR, '../static') to os.path.join(PROJECT_DIR, 'static') and STATIC_ROOT = os.path.join(PROJECT_DIR, '../static_resources') to STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_resources'). It will solve your problem.
The answer was very subtle. When I upgraded Django to 1.9 and ran the server, it gave the following warning:
?: (urls.W001) Your URL pattern '^static/(?P<path>.*)$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
In urls.py, my urlpatterns list contained:
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
I changed it to:
url(r'^static/(?P<path>.*)/', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
This eliminated the warning but caused static resources to stop loading.
It needed to be:
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
It's still a mystery to me why this worked on my dev machine (a Macbook), as well as another on the team's dev machine (a windows laptop), but not on our Linux server. But, it works now, so I'm done trying to figure it out.
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