Static Root and Static Url confusion in Django - python

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

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.

Django can't find my static files after changing to production settings and back

I've been trying to deploy my site this weekend and have thus been meddling with the settings. One of the unpleasant surprises while doing this has been that my static files have seemingly stopped working on my site. My CSS files and javascript files don't work anymore, as if they aren't found by the site. The only thing I can remember doing with regards to static files was inserting this into settings.py:
# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# The URL to use when referring to static files (where they will be served from)
STATIC_URL = '/static/'
I removed these settings and replaced them with the original
STATIC_URL = '/static/'
but alas, the problem remains. Why isn't Django finding my static files anymore?
PS: As I'm a noob, I don't know exactly what is relevant from my project for you guys to see, but do let me know and I shall provide additional info.
Okay, update your settings.py as below:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_my_proj"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")
Now, that you have done that make dir called static_my_proj, at the same level where your manage.py file is.
Okay, now create new dir called static_cdn just one level up, i.e. one level up to manage.py file.
Now, create dir called static_root inside static_cdn and that is all you have to do. Make sure you run python manage.py collectstatic
NOTE:
static_my_proj is for development or DEBUG = True and static_cdn is for production or DEBUG = True. handy, right?
Also, you can add media file the same way, just add,
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")
and make new dir called media_root under static_cdn.
EDIT:
Copy all your admin files to static dir manually.
If you have virtualenv than go to <virtualenv-dir>/lib/python3.6/site-packages/django/contrib/admin/static and copy dir named admin.
It should work even if DEBUG=False
Hope this helps.

Django Localhost not loading static files

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.

Django. settings.py -> STATIC_ROOT/STATICFILES_DIRS

Have a problem with connecting css to my template.
My project root is
"D:/birzha/", static path is "D:/birzha/static/", css in
"D:/birzha/static/css/template.css".
What STATIC_ROOT or STATICFILES_DIRS should I use for correctly viewing css file? I tried so much turns, but nothing happens, css still off.
First of all you need to tell where all your static files will be "collected", place the following lines in settings.py:
BASE_DIR = (os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
Then you need to make a static folder within your app and the hierarchy
should be like this appname/static/appname/css.
Finally run the command python manage.py collectstatic and type yes on prompt. This will copy all of your static files within a folder that you specified in STATIC_ROOT
Now you can access your static files by giving the path like,/static/appname/css/mystyle.css
You can try this
Site root will contain you project root path. You can change "/templates" with static
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
# calculated paths for django and the site
# used as starting points for various other paths
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates/'),
)
MEDIA_ROOT = os.path.join(SITE_ROOT, 'templates/media/')
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(SITE_ROOT, 'templates/'),
)

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