What are the differences of these three static url?
I am not sure if I am right, I am using the MEDIA_ROOT to store my uploaded photos (via models.ImageField())
However, I created a JS script to my admin and in admin.py. I defined the media as below:
....
class Media:
js = ('/admin/custom.js', )
and my settings.py:
....
STATIC_ROOT = "/home/user/project/django1/top/listing/static"
and I added the custom.js to STATIC_ROOT/admin/custom.js, but it is not working. Throwing 404 not found error.
And then I change the STATIC_ROOT to STATICFILES_DIRS, and it works!!
....
STATICFILES_DIRS = "/home/user/project/django1/top/listing/static"
So, I am not understand what is going on here. In fact, I just don't understand what is the difference between STATIC_ROOT and STATICFILES_DIRS.
Currently I am testing Django in my machine via virtualenv, not deployed yet, is it the reason STATIC_ROOT not working??
Development
STATIC_ROOT is useless during development, it's only required for deployment.
While in development, STATIC_ROOT does nothing. You don't even need to set it. Django looks for static files inside each app's directory (myProject/appName/static) and serves them automatically.
This is the magic done by manage.py runserver when DEBUG=True.
Deployment
When your project goes live, things differ. Most likely you will serve dynamic content using Django and static files will be served by Nginx. Why? Because Nginx is incredibly efficient and will reduce the workload off Django.
This is where STATIC_ROOT becomes handy, as Nginx doesn't know anything about your Django project and doesn't know where to find static files.
So you set STATIC_ROOT = '/some/folder/' and tell Nginx to look for static files in /some/folder/. Then you run manage.py collectstatic and Django will copy static files from all the apps you have to /some/folder/.
Extra directories for static files
STATICFILES_DIRS is used to include additional directories for collectstatic to look for. For example, by default, Django doesn't recognize /myProject/static/. So you can include it yourself.
Example
STATIC_URL = '/static/'
if not DEBUG:
STATIC_ROOT = '/home/django/www-data/example.com/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
You can find these settings in the Django documentation. Here are my own definitions and quotations from the documentation:
MEDIA_ROOT is the folder where files uploaded using FileField will go.
Absolute filesystem path to the directory that will hold user-uploaded files.
STATIC_ROOT is the folder where static files will be stored after using manage.py collectstatic
The absolute path to the directory where collectstatic will collect static files for deployment.
If the staticfiles contrib app is enabled (default) the collectstatic management command will collect static files into this directory. See the howto on managing static files for more details about usage.
STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.
This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.
In your settings, you should have:
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# Make a tuple of strings instead of a string
STATICFILES_DIRS = ("/home/user/project/django1/top/listing/static", )
...where:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
as defined in the default Django settings.py now.
Difference between STATICFILES_DIRS and STATIC_ROOT
The STATICFILES_DIRS can contain other directories (not necessarily app directories) with static files and these static files will be collected into your STATIC_ROOT when you run collectstatic. These static files will then be served by your web server and they will be served from your STATIC_ROOT.
If you have files currently in your STATIC_ROOT that you wish to serve then you need to move these to a different directory and put that other directory in STATICFILES_DIRS. Your STATIC_ROOT directory should be empty and all static files should be collected into that directory.
MEDIA_ROOT where media files ,all uploaded files goes.
Example : Images, Files
class Media:
js = ('/admin/custom.js', )
but it is not working. Throwing 404 not found error.
The 404 error is in part because of the leading slash in the file path.
Related
This is my django project, there is two apps; polls and study
This is the setting of my static file settings.
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,"study","static","HScard"),
)
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
I expected the 'python manage.py collectstatic' in the shell would copy static files to "staticfiles" from only in "study/static/HSCard"(due to STATICFILES_DIRS above).
However, "collectstatic" copied unexpected files below.
enter image description here
Why are the files in admin and polls copied to staticfiles?? besides the files in admin folder were from here (a single example):
Copying 'C:\ProgramData\Anaconda3\lib\site-packages\django\contrib\admin\static\admin\js\vendor\xregexp\xregexp.min.js'
Why does 'collectstatic' working like this and how can I fix it as I expected?
The version of django using is 2.0.7.
Thank you.
The problem is due to the default Staticfiles Finders settings:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
The AppDirectoriesFinder will look through your apps listed in your INSTALLED_APPS setting. If there are files such as admin/static/, which are included in the admin by default, then they will also be found by collect_static. Your choices are to either:
Remove apps from INSTALLED_APPS (obviously they won't work any more, but they won't contribute static files either)
Add the STATICFILES_FINDERS setting as above, but without the AppDirectoriesFinder setting. Then Django will only collect staticfiles that you have explicitly mentioned in STATICFILES_DIRS
While I run python manage.py collectstatic
It says You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
This is my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static'),
]
How can I solve this issue?
You need to add STATIC_ROOT as a parameter to your settings.py.
Here is how it works with those static files:
In STATICFILES_DIRS you tell django where to go and look for static files. This not primarily to serve them, but to collect them! The point is that on a productive server, django is not supposed to server static files, this task should be handled by the web server. Check the docs for details. The idea is that you run the command python manage.py collectstatic and django will go through all the paths in STATICFILES_DIRS, collect the static files and put them... - exactly! - into the STATIC_ROOT folder.
At this point 2 things should become clear:
1. You need to provide STATIC_ROOT in your settings.py.
2. The path provided in STATIC_ROOT cannot be part of STATICFILES_DIRS, as this would mean django would need to put the folder into itself.
Again, this is in the case of a productive server. During development django can handle static files without help of a web server, here is more from the docs on that. Nevertheless, the STATIC_ROOT parameter is always required.
Hope that helped to clarify things. Happy coding! :)
STATICFILES_DIRS tells django where to look for files to collect when using the collectstatic command
STATIC_ROOT="../my_real_static_folder" will tell django to put the static files it collects there and the static_url will get files from there.
... however be warned serving static files through django only works if debug is True (in settings.py) and is strongly discourages in production environments
Set STATIC_ROOT = 'path to ur static root directory' in settings.py.
Note that when you run collectstatic all files from ur static dirs are collected to the static root directory provided in static root path above. U can find more info about this in documentation here
Preface this by saying I have now read multiple posts on this question (including here, here, and here). What I understand is that the static url in settings.py needs a modification for heroku to run these static files. What I need, explained like I am a child, is what tweak to make to these static url when the static directory is nested within the app -- as this was a best practice imparted in a recent tutorial (if this is not the ideal practice I would appreciate being corrected).
Question 1: Should the media files be kept in a directory within the app or at the project level?
Question 2: If the media files are kept within a directory inside the app, like my directory below, then how am I supposed to modify the url in settings.py to load the static files once pushed to heroku?
My project structure is the following:
gvlabs
__init__.py
__init__.pyc
settings.py
settings.pyc
urls.py
urls.pyc
wsgi.py
wsgi.pyc
manage.py
Procfile
requirements.txt
runtime.txt
welcome
__init__.py
__init__.pyc
admin.py
admin.pyc
apps.py
hello.py
migrations
models.py
models.pyc
static
css
fonts
images
js
templates
welcome
base.html
comingsoon.html
contact_us.html
index.html
post_list.html
tests.py
urls.py
urls.pyc
views.py
views.pyc
settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(PROJECT_ROOT,'../welcome')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
#STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_ROOT= os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/welcome/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = ()
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
The main problem with your configuration is the STATIC_ROOT setting. You should change this to something like os.path.join(BASE_DIR, 'static_root').
STATIC_ROOT should point to an empty directory (it doesn't need to exist, Django will create it if necessary) where Django can collect your static files together and do any necessary processing on them before it serves them. It is not the directory where you store your static files.
Regardless of where you put your static files, you shouldn't need to change the STATIC_URL setting. Just leave it as /static/. The main reason for needing to change this is when you're serving static files via a CDN, when it would be set to something like https://my-cdn.example.com/static/
I would keep static files in a directory at the project level. Sometimes, when creating a reusable app it makes sense to bundle everything together by storing its static files in a directory within the app. But most projects I've worked on have kept the main set of static files at the project level.
It doesn't really matter where you put your static files as long as you tell Django where to find them. You do this by adding the path to the directory to the STATICFILES_DIRS setting like so:
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
]
(Technically, if your static files are in an app directory Django should be able to find them automatically, but let's keep things simple and explicit.)
As a side note: be careful not to use the term "media" here as that has a specific meaning in Django terminology where it refers to user-uploaded files like profile images rather than files that belong with your codebase like CSS and JavaScript files.
I want to be able to load my static files on my local server, but when I request them, the browser returns 404 for every resource.
From what I can understand, STATIC_URL is the url in which my static files will be served. And STATICFILES_FINDERS specifies how my static files will be discovered. I set STATICFILES_DIRS to search for the static directory at the project root, but it doesn't seem to be be finding it.
On my settings.py,
# Python 2.7.5, Django 1.6
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
This is my directory structure:
.
|-- myapp
|-- settings.py
|-- ...
static
|-- images
|-- javascript
|-- stylesheets
Here are a couple of ideas:
You need a server for your static files. Are you using Apache HTTP server? The easiest way to serve your static files is to alias them in the httpd.conf file:
Alias /static/ /path/to/static/
<Directory /path/to/static>
Require all granted
</Directory>
You need to specify a STATIC_ROOT, which could be /path/to/your_project/static but then you probably want to put your current static files and folders somewhere else, because everything in STATIC_ROOT will be overwritten when you call manage.py collectstatic. I put all of my static files, such as Bootstrap, Tablesorter, images and icons in a folder called assets, then put assets in my STATICFILES_DIR list.
Use manage.py collectstatic to collect all static files and put them in STATIC_ROOT so that Apache can find them. Static files for the admin site will be automatically copied even if you do not add them to the list of STATICFILES_DIR.
Check out this post I wrote, which has several links to Django documentation on the topic.
I am learning django and I already have a bit noobish question. I can not point to my static folder and I tried all the combinations, watched people do it in youtube tutorials etc.
My settings.py looks something like this:
STATIC_ROOT = '/home/peter/brewery/static/'
TEMPLATE_DIRS = '/home/peter/brewery/mysite/templates/'
where brewery/ is the folder containing mysite/ and static/, mysite/ is the folder created by
django-admin.py startproject
where settings.py also lives...
It seems that templates folder is mapped correctly, since the page renders with proper templates, it just cannot access the css in the /static/css/ folder. I show the path in my template for css like this
<link ... href='/static/css/brewery.css' />
I have also tried to make href absolute path on my computer and it does not work.
I am using django 1.3 and am running the server provided by django (python manage.py runserver)
1 - In your settings file, define a static url and static root like this:
STATIC_URL = '/static/'
2 - Set DEBUG = True
3 - Make sure your TEMPLATE_CONTEXT_PROCESSORS variable includes django.core.context_processors.static.
4 - Reference it in your templates like...
<link ... href='{{ STATIC_URL }}css/brewery.css' />
Source: https://docs.djangoproject.com/en/dev/howto/static-files/
In debug mode STATIC_ROOT is not used, but staticfiles_urlpatterns() provides static files from all different apps. Put your static files either into static/ directory in one of your apps or define STATICFILES_DIRS in your settings and put static files for the site there.
STATIC_ROOT is just the location where all static files are collected from all apps and STATICFILES_DIRS when you call:
python manage.py collectstatic
And it is only used in production environment.