Django static files resolving after collectstatic - python

I have run
python manage.py collectstatic
so I have copies of my static files in "/static" folder.
But now Django uses files from /static/myapp/js/myapp.js, not from myapp/static/myapp/js/myapp.js
what should I change to resolve myapp/static/myapp/js/myapp.js first?
settings.py
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_URL = '/static/'

From the Django official documentation:
The Default value of STATICFILES_FINDERS is:
[
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
The default will find files stored in the STATICFILES_DIRS setting (using django.contrib.staticfiles.finders.FileSystemFinder) and in a static subdirectory of each app (using django.contrib.staticfiles.finders.AppDirectoriesFinder). If multiple files with the same name are present, the first file that is found will be used.
So, in your settings.py, add this:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
]
Basically what we are doing is overriding STATICFILES_FINDERS by swapping the default entries, so that AppDirectoriesFinder is used first.

Related

Django Found another file with destination path

I am trying to deploy the app on GCP but when I run the following command:
python manage.py collectstatic
It returns:
Found another file with the destination path
'admin\css\autocomplete.css'. It will be ignored since only the first
encountered file is collected. If this is not what you want, make sure
every static file has a unique path.
Found another file with the destination path 'admin\css\base.css'. It
will be ignored since only the first encountered file is collected. If
this is not what you want, make sure every static file has a unique
path.
And many other like this.
Here's my Settings.py
STATIC_URL = 'https://storage.googleapis.com/yantra-packs/static/'
# STATIC_URL = '/static/'
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), ]
Where is it that I am providing two paths for the static files?
The findstatic command will help to debug.
The default value of STATICFILES_FINDERS is:
[
'django.contrib.staticfiles.finders.FileSystemFinder', # finds files stored in the `STATICFILES_DIRS` setting.
'django.contrib.staticfiles.finders.AppDirectoriesFinder', # finds files stored in the 'static' subdirectory of each app.
]
Hence, even though you only have:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), ]
AppDirectoriesFinder will find files stored in the static subdirectory of each app.
Also, you might want to look at the Django's official documentation describing the collectstatic command.

CSS is not showing in the admin page -Django

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/'),
)

Can't use django-compress with Heroku

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 = ['*']

STATIC_URL issue in django 1.5.4

I wanted to enable serving static files through django 1.5 builtin development server. I came across a strange problem.
If I make a request to my static file using localhost:8000/static/staticstyle.css then it responds me with 404 not found. But If I make a request to the same file without the value mentioned in the STATIC_URL and add the url pattern as "url(r'^anyval_other_than_mentioned_STATIC_URL/', django.views.static.serve,{'document_setting':settings.STATIC_ROOT}), ", then it responds me with 304 , which is conditional get.
I have collected all the static files in the static directory setup in the settings file and enabled all the context processor required for the template.
For production I have used nginx to serve the static file so there is no problem.
My settings.py looks like
STATIC_ROOT = APPLICATION_PATH+"/static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('vendors',APPLICATION_PATH+'/vendors'),
('admin/assets/',APPLICATION_PATH+'/templates/admin/assets'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
My application urls.py looks like
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Any hints would be highly appreciated.
Thank you .
please read this first
https://docs.djangoproject.com/en/1.5/howto/static-files/
There are some settings related to static files serve
The list of finder backends that know how to find static files in various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Absolute path to the directory static files should be collected to
STATIC_ROOT = os.path.join(APPLICATION_PATH, '..', 'static')
URL prefix for static files.
STATIC_URL = '/static/'
You can specify Additional locations of static files
STATICFILES_DIRS = (
os.path.join(APPLICATION_PATH, 'vendors'),
os.path.join(APPLICATION_PATH, '/templates/admin/assets'),
)
There is special application for static serve in django contrib
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
After You have configured your project run collectstatic command
python manage.py collectstatic
This will copy all files from your static folders into the STATIC_ROOT directory.

Django - Static Files from App Directories

In a development environment, I'd like to use static files from the app directories.
#settings.py
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = (os.path.join(SITE_ROOT, 'static_files/'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'static/'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
#...
'django.core.context_processors.static',
#...
)
INSTALLED_APPS = (
#...
'django.contrib.staticfiles',
#...
)
I can find my static file if located in /static/css/file.css but not if in an_app/static/css/file.css.
Ok I found the problem, I made an oversight that was not visible in my question. I was searching into in static/js, static/flash, static/css, static/images and put the files directly into app/static so they were not found.
Check the value of STATIC_ROOT and STATICFILES_DIRS.
Add a print in your setting file.

Categories