Django 2.2.4 unable to serve my static files - django

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

Related

How to make static directory in Django

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)

Django can't load static files even though all settings are fine

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)

Static file collections - Logo navbar does not show up

I am trying to have an image (logo) on the navbar.
My project is called "mysite-project" (where manage-pyis), it contains the app "mysite".
In order to upload my static file I did the following:
1) mysite-project/mysite/settings.py
I added:
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/')
]
2) Created folders static and added my logo.png in:
mysite-project/static/mysite-project/logo.png
3) mysite-project/templates/base.html
{% load staticfiles %}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url 'home' %}">
<img src="{% static 'mysite/logo.png' %}" height=30 width=30 class="d-inline-block alighn-top" />
Code of Conduct
</a>
</nav>
4) In mysite-project/mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('users/', include('django.contrib.auth.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
HOWEVER the image does not show up. I think I have some issues in the settings.py for the folders but I cannot find where
The issue is in your STATICFILES_DIRS setting. If you join a path that has a leading slash then the result will "ignore" any preceding arguments and everything after will be relative to root
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/static/') # This will result in "/static/"
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static') # This will result in "<BASE_DIR>/static/"
]
STATIC_ROOT is the directory that you want to serve files from and the directory that collect_static will populate with all your static files, STATICFILES_DIRS is where Django will collect the files from. STATICFILES_DIRS shouldn't contain STATIC_ROOT. The usual layout for a project is something like this
myproject/ # The root of your repo
myproject/
myapp/
static/ # This is where you put app specific assets
...
static/ # This is where you put your generic static assets. Add this to STATICFILES_DIRS
...
static/ # This is STATIC_ROOT and where your files are served from after being collected
The default value for STATICFILES_FINDERS will look in STATICFILES_DIRS and every apps static directory. If you are using git, you should add the static folder at the root of your repo to .gitignore

How to manage static files from Django 1.8 to Django 1.10

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'),
)

Django static css file

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.