CSS is not showing in the admin page -Django - python

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

Related

django.core.exceptions.SuspiciousFileOperation: The joined path is located outside of the base path component

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.

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

django1.9 does not load css from static admin

I am currently using django version 1.9. I try to create a new superuser then I run the server and try to login through the browser by navigating to 127.0.0.1:8000/admin, but the django admin page seem does not have any css. When i do inspect element in the browser itself I come to know that it link to two css files one is base.css and another one is login.css but those two contain nothing when i try to view it from the browser. After that i try to find those file in the installed django directory and i found out the base.css and login.css then i copy the all the code in that file to the base.css and login.css which i opened in the browser, then i got the beautiful django login page with proper css. I don't know what to do with this.
This is my console:
Here is the screenshot
I am using python 3.4.3 and django1.9.0. Thanks
You have to set STATIC_ROOT, STATIC_URL and STATICFILES_DIRS in settings.py as below:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
and then try to run:
python manage.py collectstatic
If you are using django-rest-framework see this.
You need to add it as,
INSTALLED_APPS = (
...
'rest_framework',
)
and in urls.py:
urlpatterns = [
...
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
It will automatically search and catch all the static files.

Unable to perform collectstatic

I am new to django ! When I use the command python manage.py collectstatic I get this error
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path
But I can successfully run the server .
My static files declarations are :
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('assets', os.path.join(PROJECT_DIR, '../static')),
)
and debug is set to true
DEBUG = True
How can I fix this? Else am missing any installation packages ?
Try this,
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
Look at https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATIC_ROOT
You must have to give path in STATIC_ROOT in settings.py where all your static files are collected as for example:-
STATIC_ROOT = "app-root/repo/wsgi/static"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('assets', 'app-root/repo/wsgi/openshift/static'),
)
you can create 'static' folder in any subfolder and have required files in it.
In settings.py add the following lines of code:
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'
After running
python manage.py collectstatic
a new static folder will be created in your parent App folder
well had this error as well. I fixed:
STATIC_URL = '/static/'
if DEBUG:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
else:
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'assest')
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')
]
I had to put STATIC_ROOT and STATIC_URL above the STATICFILES_DIRS declaration.
STATIC_ROOT = "/var/www/YourSiteFolder/static/"
STATIC_URL = '/static/'
look at https://docs.djangoproject.com/en/1.11/howto/static-files/#deployment
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
This works for me
if you want to load static files rather than admin panel files or getting errors while loading webpage static files like CSS js etc
I suggest you change the folder name of 'static' to 'staticfiles'
and then add this code in your settings.py
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
then after run python manage.py collectstatic
Then the problem will be fixed

Managing static files in Django

STATIC_ROOT = '%s/site_media' % PROJECT_DIR
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(STATIC_ROOT, 'images'),)
When I run 127.0.0.1:8000/static/something.jpg everything works.
But I want get something.jpg like this 127.0.0.1:8000/static/images/something.jpg
When I change STATICFILES_DIRS:
STATICFILES_DIRS = ('',)
127.0.0.1:8000/static/images/something.jpg doesn't work, why?
Use
STATICFILES_DIRS = (os.path.join(STATIC_ROOT, ''),)
Your STATIC_DIR should point to the project site_media dir. When adding images' to it, then 127.0.0.1:8000/static/images/something.jpg is being searched in .../site_media/images/images/something.jpg

Categories