Here is the error I receive when trying to load {{STATIC_URL}}img/memestatlogo2.jpg. Why is django not looking in STATICFILES_DIRS for img/memestatlogo2.jpg?
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/img/memestatlogo2.jpg
Using the URLconf defined in memestat.urls, Django tried these URL patterns, in this order:
^$
The current URL, static/img/memestatlogo2.jpg, didn't match any of these.
Here are relevant configurations form my settings.py
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('/home/ryan/Programming/OpenCV-2.4.2/msheroku/memestat/stats/')
The image is then located in
'/home/ryan/Programming/OpenCV-2.4.2/msheroku/memestat/stats/img/memestatlogo2.jpg'
STATICFILES_DIRS is only used when you execute the collectstatic command. Files are then copied into STATIC_ROOT which in the example above is unset.
You need to set STATIC_ROOT to a dir where you want your static files to be copied. This dir also holds your project-wide static files that are not part of any app like perhaps robots.txt, favicon.ico, your logo(s) and background images.
Related
I am trying to deploy the app on Heroku but when I run the following command: "python manage.py collectstatic" it returns multiple "Found another file with the destination path 'admin...".
When I "git push heroku" the app, I can see the same error.
Next, the application works, but /admin page doesn't have any format, like it is not reading css, font, etc. I've checked staticfiles/admin and it has its css,font,img and js folder with files.
I've tried all the advices given here, but I'm not finding a solution. Here my settings (see my comments at the end of the code):
import os
import django_heroku
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
.
.
.
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') #Heroku
#STATIC_ROOT = os.path.join(BASE_DIR, 'static') #ok for IBM Cloud
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media') # if I comment this line and
MEDIA_URL = '/media/' # this line, /admin page has correct css but I don't have images in the application
# Extra places for collectstatic to find static files.(extra heroku)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
# Next line makes application not work, it shows database errors
django_heroku.settings(locals())
"""
so, I added "release: python manage.py migrate" to procfile, now the app works,
/admin shows format ok, but database seems to be empty, no information is displayed on
site pages, and admin users don't exist anymore (my database is sqlite3).
"""
And what I get when "git push heroku master" is:
remote: Found another file with the destination path 'admin/img/selector-icons.svg'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
remote: Found another file with the destination path 'admin/img/calendar-icons.svg'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
.
.
.
Please help
I have started to develop a Django project with currently 3 apps (registration, monitor and randomization)
I wand to set parameters for statics files.
I am using bootstrap 4.0 and I have currently only one css files for sticky footer
as this css is for all template of the project, I have created a static folder at the project's root:
-project
- settings.py
- monitor
- randomization
- registration
- static
- css
- styles.css
- images
and in my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
STATIC_ROOT = 'D:/Users/jl3/DevSpace/intenseTBM_eTool/static/'
I have run the commande collectatic but I have an error:
ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
how can I set 'global' static files for my project?
how should I set parameters?
Well the error is pretty clear.
Also, if the STATIC_ROOT is the same as in STATICFILES_DIRS, there is no need to set it in your settings. STATICFILES_DIRS is the list of folders where Django will search for additional static files, in addition to each static folder of each app installed. STATIC_ROOT is the folder where every static files will be stored after a manage.py collectstatic.
I'm working in a development environment with debug = True on Windows 10.
I have installed node and npm and run npm microm from the main folder of my project. It created a node_modules subfolder with a microm folder inside, among others. so the microm.js script is located in:
f:\roomtemp\node_modules\microm\microm.js
The relevant portion of my Settings file is:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'node_modules')
My template contains the following:
{% load staticfiles %}
<script src='{% static "/microm/microm.js" %}'></script>
When I attempt to load the page, the Chrome console shows a 404 and I see this in the server console:
"GET /static/microm/microm.js HTTP/1.1" 404 1661
I have read many SO questions that seem to contain the solution but none have worked so far. I've also searched high and wide on the internet. No luck so far. Any help would be much appreciated.
Mike
First of all I don't think you should STATIC_ROOT at your node_modules folder. STATIC_ROOT is where the django management commant collectstatic will put all the static files from all the apps into when you are readying for deploying. This process may result in some files being overwritten.
Please restore STATIC_URL to some sane value like
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
Then use the STATICFILES_DIRS setting. This is what tells django what extra locations to look for static files.
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'node_modules')]
Finally, make sure that your STATICFILES_FINDERS has these
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
I'm using a webfaction-hosted django site, and I'm trying to get locally stored images to show properly, but I'm having trouble getting the static directories/permissions/etc. to line up.
I've configured everything according to webfaction's guide at https://docs.webfaction.com/software/django/config.html
In my project's templates folder, I have the line:
<img src="{{ STATIC_URL }}{{ project.overview_image }}">
where project.overview_image is a CharField where I can list a subdirectory where the image is located.
In my settings.py, I have:
STATIC_URL = 'http://morphogen.cc/static/'
STATIC_ROOT = '/home/scottnla/webapps/static_media/'
STATICFILES_DIRS = (
'/home/scottnla/webapps/portfolio_website/portfolio_website/static/'
)
where /webapps/static_media/ is my static media app.
inside of my project's local /static/ folder, i have an image in the folder
/home/scottnla/webapps/portfolio_website/portfolio_website/static/images/small/img.jpg
and when i run 'manage.py collectstatic', the image is copied to:
/home/scottnla/webapps/static_media/images/small/img.jpg
but when I look at my served HTML page, the image doesn't show.
When I inspect the element, I see that the image source is listed as:
<img src="http://morphogen.cc/static/images/small/img.jpg">
which seems consistent with everything above, but if I go directly to that directory, I get a 403 Forbidden Error.
What's the next step in troubleshooting this?
thanks.
Your settings.py should look like this.
STATIC_ROOT = '/home/USER_NAME/webapps/static_media/'
STATIC_URL = '/static/'
STATIC_DIR is outdated and unnecessary for a single static folder
ALSO, you must have
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
IF you're still having problems check the Django Static Files Documentation
I am from PHP background and want to give path to
STATICFILES_DIRS
in settings.py and in windows I understand that I will need to give full path like:
D:/path/to/folder/project/static
I want to know that is there a way to give some path like "/static/" or can we give path like dirname(__FILE__) in PHP?
So that my path is not hardcoded. Also why it is physical path as webserver normally don't follow physical path for loading CSS files as it use http path? So why it is this way(physical path) in django? Can some one please explain.
thanks for everyone's effort in advance.
In my settings.py files, I always do
import os
ROOT_PATH = os.path.dirname(__file__)
Then you can do
STATICFILES_DIRS = [os.path.join(ROOT_PATH, 'static')]
The reason that STATICFILES_DIRS wants a filesystem path is that it needs to know where the files live in the operating system (so things like manage.py collectstatic are possible). The STATIC_URL setting is for webserver paths, and that's what it uses to show to the user in the admin or the {% static %} tag or whatever. STATICFILES_DIRS is server-side only and never shows up in any rendered templates or anything.
Find the settings.py file in the project,
Put:
STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'))
Change to:
STATICFILES_DIRS=[(os.path.join(BASE_DIR,'static'))]
Answer on your question is wrong. If you do like this, you'll get the next error:
ImproperlyConfigured: Your STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma?
My decision is add to top of settings.py file the next function:
import os
def look_folder_tree(root):
result = ()
for dir_name, sub_dirs, file_names in os.walk(root):
for sub_dir_name in sub_dirs:
result += (os.path.join(dir_name, sub_dir_name),)
return result
# Django settings for project.
PROJECT_DIR = os.path.dirname(__file__)
and call look_folder_tree function at:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = look_folder_tree(STATIC_ROOT)
My decision is allow to add all sub folders inside static directory.
In settings.py :
Import os (already done when you create a django project)
import os
You can just use STATICFILES_DIRS as a tuple, mind the trailing comma.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Or you can use the STATICFILES_DIRS as a list data-type, comma at the end is not required here:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
Change this:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'))
To This:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Add a comma at last as,
ERRORS:
?: (staticfiles.E001) The STATICFILES_DIRS setting is not a tuple or list.
HINT: Perhaps you forgot a trailing comma?
your syntax is correct, I think you forget of importing os at the first line of code as import os.
If you are using newer versions of Django i.e Django 3.1, and your BASE_DIR is like this ...
BASE_DIR = Path(__file__).resolve().parent.parent
Then do this :-
STATICFILES_DIRS =[BASE_DIR / 'my_app/static',]