I don't know why django can't load static files even though I have defined static_url, static_root, staticfiles_dirs and urlpatterns including static_root. I put static files in root_dir/static/ directory.
My templates also got right syntax with {% load static%} and {% static '...'%}
Please give me some advice on this.
Many thanks
This is my main urls file
from django.contrib import admin
from django.urls import path
from home import views as HomeViews
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',HomeViews.index,name='index')
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
check if your program has the following :
settings.py :
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
dont forget to load this tag in your template file
{% load static %}
check for the call syntax
<link rel="stylesheet" href="{% static 'base.css' %}">
check if you have created the static folder in the base directory ( where you have your manage.py file)
Check if the contents in the static file and the call corresponds with each other (check spellings)
once try to make migrations and migrate
still if not working .... Try hard refresh (ctrl + F5)
Related
when deployed server then static files not working and media files not diplaying. 404 error
here is the urls.py
from django.views.static import serve
import django
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path ('' , include('home.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings.py
STATIC_URL = 'static/'
STATIC_ROOT = '/usr/local/lsws/Example/html/demo/static'
"""STATICFILES_DIRS=(
BASE_DIR / "static",
)"""
MEDIA_URL = 'media/'
MEDIA_ROOT = '/usr/local/lsws/Example/html/demo/static/media'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CKEDITOR_UPLOAD_PATH = 'uploads'
Verify that on prod it's the same folder and the user running webserver has access to this folder. Consider using BASE_DIR instead of absolute paths. Also you might prefer to store projects files not inside user private home folder and store them somewhere /var/opt or whatever. If you had media files locally before deployment then they must be copied to prod manually. –
Thank you #Ivan-Starostin
I created static directory which included css and image files in my Django project. When I run my project the css do display("GET /static/website/barber.css HTTP/1.1" 200 69) but the images do not("GET /static/website/images/balloons.gif HTTP/1.1" 404 1840),
in your setting.py file define STATIC_URL
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
os.path.join(BASE_DIR,'boot'),
]
STATTC_URL = '/static/'
and add this to your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#your url
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Add STATIC settings
You need to create these definitions in settings.py to tell Django where is static files:
STATIC_DIR = os.path.join (BASE_DIR, "static")
STATIC_ROOT = os.path.join (BASE_DIR,'static files')
STATIC_URL ='/static/'# path to read css with local (probably)
STATICFILES_DIRS = [
os.path.join (BASE_DIR, "static"),
]
Be sure to import settings and static in urls.py.
You also need to add them to your main urls.py to access the URLs of images:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#Your urls
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In my application I was unable to serve static files like images and js.
Looks like everything is correct but still I am unable to figure out why it is not loading.
I have tried few options in stackoverflow none of them worked for me.
Settings.py
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static")
]
STATIC_URL = '/static/'
My project structure
mydjangonginx
|-mydjango_app
| -urls.py
| -views.py
| -...
|-mydjangonginx
| -urls.py
| -settings.py
| -...
|-static
| -images
| -login.jpg
| -js
| -my.js
mydjango_app/urls.py
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('login', views.login, name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
mydjangonginx/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('mydjango_app.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
login.html
<html>
{% load static %}
login
<script src="{% static 'js/my.js' %}"></script>
<img src="{% static 'images/login.jpg' %}" />
</html>
both the js and images are not loading
can anyone help on this
The static folder should be next to manage.py in the same folder (this is BASE_DIR if you did not change it).
'django.contrib.staticfiles' is in your INSTALLED_APPS? And you are using the {% static 'folder/some.file' %} template tag to serve the static files?
Also note that adding staticfiles to the urlpatterns is not needed when running in DEBUG mode.
Edit: it has to be STATICFILES_DIRS not STATICFILES_DIR
I have troubles with upgrading my project from Django 1.8 to Django 1.10: static files are not loaded anymore.
My template looks like this:
{% load staticfiles %}
<!DOCTYPE html>
...
<link href="{%static 'file.css' %}" rel="stylesheet">
...
Then in my file settings.py, I have 'django.contrib.staticfiles' as an installed app. DEBUG is set to True, and I have:
STATIC_URL = os.path.join(BASE_DIR, 'static/')
STATIC_ROOT= os.path.join(BASE_DIR,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), "./", ]
But when the html is produced, it is like the %static has no effect anymore. It is replaced by the empty string (the same works fine with Django 1.8, where the %static is replaced by the content of STATIC_URL). Does anyone know how to fix this ?
Can you added the Update the urls.py(mainproject/urls.py), once you made the chnages run the python manage.py collectstatic command.
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
===================
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
def root(folder):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',folder)
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
root('static'),
)
Project
myblog-
|
|-static
|
|-Css
|
|-bootstrap.css
** Setting.py**
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
**urls**
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from myblog.views import hello, current_datetime, hours_ahead
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$',hello),
url(r'^time/$', current_datetime),
url(r'^time/plus/(\d{1,2})/$', hours_ahead),
url(r'^', include('blog.urls')),
url(r'^blog/',include('blog.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += staticfiles_urlpatterns()
Templates/base.html
{% load staticfiles %}
<link href="{% static 'base/css/bootstrap.css' %}" rel="stylesheet" type="text/css" >
After running manage.py runserver 192.168.8.213:8080. It shows Page not found (404) Request Method: GET Request
URL: http://192.168.8.213:8080/static/base/css/bootstrap.csenter code heres Raised by: blog.views.index
'base/css/bootstrap.css' could not be found
You're seeing this error because you have DEBUG = True in your Django
settings file. Change that to False, and Django will display a
standard 404 page.`enter code here`
We have to load static file into individual app, when we are on debug mode.
And In project name aPP folder, we will load the static folder
In setting.py
We have to define the path
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'myblog/static'),
)
In production
If we write "python manage.py colltecstatic" ,it will copy the static file in root static folder.