To serve the static files (CSS, JS), I set the settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
This means that all my static files need to be in the project root's static folder
/app1
/app2
/media
/static #They need to be stored here, all static files
/templates
urls.py
settings.py
manage.py
However, I am storing them in my app's static folder.
/app1/static/ # Storing the static files here
/app2/static/ #and here
/media/
/static/ # but not here
Still, Django is able to serve them, how is that possible?
I tried the same thing with media files; setting
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and storing the media (images etc) files in each app's individual media dir. This time, Django did not serve the files and served them only when either I moved the files to the project root's media dir or changed the setting to MEDIA_ROOT = os.path.join(BASE_DIR, '<app_name>/media')
Why was I allowed to serve static files even when they weren't in the project root static dir but not the media files - they were only served from the root media dir.
Please read the documentation carefully - https://docs.djangoproject.com/en/1.9/ref/settings/#staticfiles-finders
It precisely states that django will search for static files in each app + the directory stated in settings.
Quoting as in the documentation -
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.
While this is not true for media files. Django doesn't look for media files in subdirectories.
Related
I need to refresh a js file in my static files folder, however whenever I run python manage.py collectstatic, a clone of my static files folder is created in a diffrent part of the project, and the js file is still not updated. I tried changing the STATIC_ROOT variable in the settings.py file to the actual location of my static folder, but it doesn't seem to refresh because the collectstatic warning message in the console says its going to be saved in a completely different location.
The Structure of my project is
resume_V6-test
-resume
-home
-static (actual location)
-resume
-settings.py
-store
-staticfiles (clone of the static folder)
-users
and my settings.py is
# Commented out the location it was before I tried fixing the error
#STATICFILES_DIRS = [
#os.path.join(BASE_DIR, "static"),
#'/resume_V6-test/resume/home/static',
#]
#STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_ROOT = os.path.join(BASE_DIR, 'home/static')
STATIC_URL = '/static/'
And the error message is
python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
C:\resume_V6-test\resume\staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Thanks
STATIC_ROOT is the output destination for the static file. E.g. this is where collectstatic collects all the static files to (read here https://docs.djangoproject.com/en/3.0/howto/static-files/)
The default is to collect all static files from all apps from /static directory of each app.
If you want extra location to collect from, you can use the STATICFILES_DIRS settings. (https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-STATICFILES_DIRS)
I am trying to handle the django static and media content. This is my code in settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
In urls.py
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am uploading files and videos to my site, everything is working fine even static and media content is being displayed.
My actual doubt is-
I am actually running in development environment. In production for collecting the static content generally python manage.py collectstatic is used and then it is handled. But in development according to docs, app called django.contrib.staticfiles is used serve static files. But what about media files, how are they served actually ??
When I upload a image it is getting stored to project_name/media/app_name insted of project_name/app_name/media/app_name as static files are being accessed from path similar to latter, why are media files being stored in different manner.
Finally:
How the media files got served in development ?
How to serve media files in production ?
When I upload a image it is getting stored to project_name/media/app_name insted of project_name/app_name/media/app_name as static files are being accessed from path similar to latter, why are media files being stored in different manner.
Media files are uploaded to the MEDIA_ROOT directory. Your MEDIA_ROOT is media, so django will keep uploaded files there.
Additionally, if in the FileField or ImageField you use upload_to path, django will create required folders accordingly. For example, for something like this:
image = models.ImageField(upload_to='myapp/')
Django will create a myapp folder inside media and keep the uploaded images there.
How the media files got served in development ?
Because of this line of code:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
It tells django to serve MEDIA_ROOT directory at MEDIA_URL.
How to serve media files in production ?
Configure your webserver to serve media and static files. Django doesn't serve media and static files because it's not good at that. An actual webserver like Nginx or Apache is configured for serving static files efficiently.
I have a Django live version in production and I develop in local with Django manager.py.
I'm not able to find the correct configuration settings for my static files.
All my static files are on a /static/ on the root of the project, because all the sub-apps use the same stylesheets and so on.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
On my live version : it works because I've got an NGINX rule that allow access to /static/ directly.
But when I want to work on local, the static files are 404, with Debug True and False.
I don't see the interest for my needings of the staticfiles directory (but I can have a wrong mind here).
Is it possible to have all the static files in subdirectories in /static/ (css, img, js, etc.) and to set a workable settings on both local and live production without switching something in settings.py for deployment ?
Thanks for your help :)
I've found the solution :
After moving my static directory from the root directory of Django the folder of my main app, I made a collectstatic with that settings :
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
My site works well in local but when I put it on my server, it can't load the static files. Here's my files.
settings.py
DEBUG=False
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
All my static are in /myproject/mymainapp/static
I've got another directory "static" of the root of my project by doing manage collectstatic in production.
Here's my nginx conf file :
[...]
root /home/user/site/mymainapp/;
[...]
location /static {
alias /home/user/site/mymainapp/static;
}
I've tried to set the path to my static directory in my root directory project but it don't make anything better.
My URLs patterns :
urlpatterns = [
[...] nothing relative to static files
]
Maybe I should add something in my URLs patterns ?
Thanks for your help :)
i have in my settings.py used
STATIC_ROOT = '/static/'
to collect the static files
this is my static_path settings
STATIC_PATH = os.path.join(BASE_DIR,'static')
when i run the collect static code django makes a static folder outside my workspace folder,should't it collect all the static files in the static folder which i have already created?
I think you've confused the STATIC_ROOT and STATIC_URL and STATICFILES_DIRS setttings.
To tell Django to serve your static files from example.com/static/, set
STATIC_URL = '/static/'
You shouldn't set STATIC_ROOT = '/static'. This is telling Django to collect your static files in /static/ on your disk, which is outside of your workspace.
STATIC_ROOT is the directory where the static files will be collected. It should be something like
STATIC_ROOT = '/var/www/example.com/staticfiles/'
If you don't want to hardcode the directory, you could do something like:
STATIC_ROOT = 'os.path.join(BASE_DIR, "staticfiles"),'
You would then configure your webserver (e.g. Nginx or Apache) to serve the files in the STATIC_ROOT.
Finally, STATICFILES_DIRS is a list of locations that Django will search for static files when you run collectstatic. The files will be copied from that directory to your STATIC_ROOT. Therefore you need to make sure that the STATIC_ROOT is not included in STATICFILES_DIRS.
Basically you can create multiple 'static' folders for different applications of Django project and those can be used in development. But whenever you go to production servers and to serve the all static files from the same place then you need this functionality in which you can collect all static files from different directory to one directory out of your workplace.
project_name
- project_name
- static
- static files goes here which are common
- __init__.py
- settings.py
- urls.py
- application_name
- static
- static files goes here which are related to application only
- __init__.py
- settings.py
- urls.py
- static - This is common place where all static files will be placed after collectstatic command for productions only.
According above structure you can understand you can server static files using different server like CDN.
Let me know if you are not satisfied with answer. Thanks.