Problem is STatic file not load.
CRM/
| |-- CRM/
| |-- next_crm/
| |-- static/
| | +-- css/
| | +-- bootstrap.min.css <-- here
| +-- manage.py
This is my static variables defined in the
next_crm/settings/settings.py file.
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
In your primary project folder (CRM, in this case) you have added the urls.py static debug lines?
urls.py
from django.conf import settings
...
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My settings.py are:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
I also face this type of problem few days ago.
but found a solution for this.Add these lines to load static in settings.py
STATICFILE_DIRS = (os.path.join(BASE_DIR, "static"), "static")
instead of this:
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
Hope this will solve your problem. :)
Related
This is my first Django project. I cannot load CSS files onto my website. i've done everything possible.
Project Directries
firstproject/
assets/
static/
css/
<CSS files>
Settings.py/ static files
STATIC_URL = 'static/'
STATICFILE_DIRS = [
os.path.join(BASE_DIR, "static")
]
STATIC_ROOT = os.path.join(BASE_DIR,'assets')
urls.py
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
HTML file
<link rel="stylesheet" href="{% static 'css/bootstrap-grid.css' %}">
I think you made spelling mistake in staticfilesdirs in settings.py file.
Change this:
STATIC_URL = 'static/'
STATICFILE_DIRS = [ #Here you made spelling mistake. It should be `STATICFILES_DIRS`
os.path.join(BASE_DIR, "static")
]
To this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
And in link tag just add type="text/css".
I did run the python manage.py collectstatic code, and after that all the static files were collected in the static folder. but the static files needed for the syling of the admin panel are not working. Could you help?
settings.py
STATIC_URL = 'static/'
STATICFILES_DIRS = [
'Portfolio/static/',
'Portfolio/',
''
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Comment out the STATIC_ROOT
I think your STATICFILES_DIRS is wrong change it to os.path.join(BASE_DIR, 'static')
Try to change STATIC_URL. for example: /Portfolio-Deployment-Folder/static/
Try to change localhost port for example:
python3 manage.py runserver 9000 or 7000
most probably problem of STATICFILES_DIRS
And finally a full example for STATIC in my personal project:
STATIC_URL = '/myproject/static/'
# STATIC_ROOT = BASE_DIR / 'static'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
This error occurs when performing command python manage.py collectstatic:
0 static files copied to '/home/project'.
That means there is no changes and my static files already exist in the destination but in this folder only one file:
static/
- staticfiles.json
But I want all my CSS, js, HTML for panel admin to be in it.
Django Settings:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = (
STATIC_ROOT,
)
urls.py
...
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I also tried these 1, 2 but no results were found.
settings.py
import os
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
urls.py
urlpatterns = [
path('demo/',Demo.as_view(),name='demo')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.STATIC_ROOT)
Add Your File in folder-
static->demo_folder->staticfiles.json
python3 manage.py collecstatic
Define all paths with STATICFILES_DIRS where Django will look for static files.
For example:
STATICFILES_DIRS = [os.path.join(BASE_DIR, "templates/staticfiles")]
When you run collectstatic, Django will copy all found files in all your paths from STATICFILES_DIRS into STATIC_ROOT.
In your case, Django will copy all files to:
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
After that, your static files are accessible through your defined STATIC_URL URL. In your case:
STATIC_URL = '/static/'
I got the 404 error to images.
https://example.herokuapp.com/images/IMG_2060.JPG 404 (Not Found)
This is my tree of directory.
directory1
|
|-- manage.py
|
|-- build/
| |
| |--static/
| .
| .
|
|-- static/
. |
. |--images/
. .
. .
The images that I want to see are in directory1/static/images/.
settings.py
INSTALLED_APPS = [
'whitenoise.runserver_nostatic',
...
]
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
....
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
whitenoise version is 5.2.0. I deployed this app to Heroku. Everything works fine except images. Did I miss something?
Thank you in advance! :)
Django's static file handling can be a bit confusing.
STATIC_ROOT is supposed to be an empty directory which Django copies your static files to ready for serving. This is done by the collectstatic command which is run automatically by Heroku.
Directories you want it to copy files from should be listed in STATICFILES_DIRS.
So if you change your settings like this it should work:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static'),
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
I have an API that delivers images and puts them in a folder. I want to make that folder accessible via Django.
folder structure:
- theProject
- static
settings.py
urls.py
.
.
.
- theApp
- apiimagesfolder
views.py
forms.py
urls.py
.
.
.
I tried adding the apiimagesfolder folder to my static dirs:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "theProject", "static"),
os.path.join(BASE_DIR, "theApp", "apiimagesfolder"),
]
and I tried accessing an image in apiimagesfolder on the debug server with 127.0.0.1:8000/imagename.bmp but failed. What would the correct path be?
Is the path staying the same after deploying?
Try this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "apiimagesfolder"),
]