how do I configure my staticfiles in django? - python

I followed the procedures to setup staticfiles but I'm not getting what i want
when I add an image to the section background it gives a wrong path "staticfiles/css/assets/img/oilcarriage.jpg"
instead of "staticfiles/assets/img/oilcarriage.jpg"
here's the path to the image:
i would be really grateful if you can help me now :)

You have to join the path so use the following STATICFILES_DIRS = (os.path.join(BASE_DIR, 'staticfiles'),)

Here you go static file config in settings.py file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
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.

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

Static Root and Static Url confusion in Django

I am trying to read create mp3 files in django. but I am confused about static and static_root that I have configured.
WHat happening is that in my code at a point when I print the below line it shows
/usr/local/src/mena_recording/play/static/audio/dorris_0_.mp3
code:
print settings.BASE_DIR+'/play/static/audio/'+record.driverName +'_'+str(counter)+'_'+ '.mp3'
but when I use the same thing in the next line in this piece it gives this error:
IOError at /
[Errno 2] No such file or directory: u'/usr/local/src/mena_recording/play/static_root/play/static/audio/dorris_0_.oga'
code:
with open(settings.BASE_DIR+'/play/static/audio/'+record.driverName +'_'+str(counter)+'_'+ '.mp3', 'w') as mp3_file:
mp3_file.write(decoded_mp3_str)
mp3_file.close()
my settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'play/static_root')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'mena_recording/static'),
os.path.join(BASE_DIR, 'play/static'),
# 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.
)
Would someone enlighten me please how this works ?
Thank you.
From the django docs,
STATIC_ROOT is the absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_URL is the URL to use when referring to static files located in STATIC_ROOT.
So, when you request some specific static resource, it is searched in STATIC_ROOT + STATIC_URL and then served.
Now in your problem, you do
STATIC_ROOT = os.path.join(BASE_DIR, 'play/static_root')
STATIC_URL = '/static/'
which means django would have effectively been searching in BASE_DIR/play/static_root/static/ which would be incorrect, so looking at other paths you can figure out that you need to do
STATIC_ROOT = os.path.join(BASE_DIR, 'play/')

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