Site on django doesn't load static - django

My site on django doesn't load static files
base.html:
{% load static %}
<link rel="stylesheet" href="{% static 'bootstrap/dist/css/bootstrap.css' %}">
<script src="{% static 'jquery/dist/jquery.min.js' %}"></script>
<script src="{% static 'bootstrap/dist/js/bootstrap.min.js' %}"></script>
files:
from terminal:
[13/Mar/2022 03:36:26] "GET / HTTP/1.1" 200 24975
[13/Mar/2022 03:36:26] "GET /static/bootstrap/dist/css/bootstrap.css HTTP/1.1" 404 1758
[13/Mar/2022 03:36:26] "GET /static/jquery/dist/jquery.min.js HTTP/1.1" 404 1751
[13/Mar/2022 03:36:26] "GET /static/bootstrap/dist/js/bootstrap.min.js HTTP/1.1" 404 1760
[13/Mar/2022 03:36:26] "GET /media/cache/77/1c/771c04f6935d264617dd3dec309a41d0.jpg HTTP/1.1" 404 1773
What could be be the reason of this?

Alxender, you should write these link and scripts in the base.html file and whenever you want static in a file write {% load static %} each time in each file.
You should also run this code in the terminal before running the server
python manage.py collectstatic
You should also check if you have set these settings in project/settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (str(BASE_DIR.joinpath('static')),)
STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

From the comments:
With debug= false django will not serve static files. That is intended behavior. > See docs.djangoproject.com/en/4.0/howto/static-files
Thanks to #Razenstein for answer!

Related

"GET /static/css/stylesheet.css HTTP/1.1" 404 1813

I tried to ling CSS file in Django framework by using " "
It shows error
but, it is showing error ""GET /static/css/stylesheet.css HTTP/1.1" 404 1813file Oder"
define STATIC_URL in settings.py like that:
STATIC_URL = 'static/'
and template file must be like that
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'app/css/style.css' %}">
Did you use following command after adding static files?
python manage.py collectstatic

Django Static wont Load

I am learning how to use django but having difficulties using static files.
Files (BASE_DIR is website, App is player):
Website
└─── player
└─── static
└─── style.css
└─── admin
In Settings:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'player/static')
In index.html:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{ static 'style.css' %}" >
In style.css:
body {
background-color: blue
}
I have ran collectstatic.
When loaded, the HTML appears without the css.
The console reads:
[23/Nov/2016 23:33:13] "GET / HTTP/1.1" 200 278
Not Found: /{ static 'style.css' %}
[23/Nov/2016 23:33:13] "GET /%7B%20static%20'style.css'%20%%7D HTTP/1.1" 404 2172
Did you run the python manage.py collectstatic?
Wrong directive in template:
{% load static %}
should be
{% load staticfiles %}
Update
also not:
{ static 'style.css' %}
but
{% static 'style.css' %}
Basic spelling errors

Django 1.5 does not Get static files in development

I am new to Django and really confused about this.
Here are relevant parts in settings.py:
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = 'http://http://127.0.0.1:8000/static/'
STATICFILES_DIRS = (
PROJECT_ROOT+'/static/')
TEMPLATE_DIRS = (
PROJECT_ROOT + '/templates/')
My project file structure is like this:
MyProj
manage.py
MyProj
settings.py
urls.py
templates
base.html
static
css
js
In base.html starts with:
{% load staticfiles %}
{% load i18n %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap, from Twitter</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link rel="shortcut icon" href="http://twitter.github.com/bootstrap/assets/ico/favicon.ico">
<link href="{{ STATIC_URL }}css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<link href="{{ STATIC_URL }}css/bootstrap-responsive.min.css" rel="stylesheet">
</head>
<body>
but django does not get none of the static files:
[15/Oct/2013 05:36:06] "GET /accounts/login/ HTTP/1.1" 200 3402
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap-responsive.min.css HTTP/1.1" 404 2122
[15/Oct/2013 05:36:06] "GET /static/js/jquery.min.js HTTP/1.1" 404 2074
[15/Oct/2013 05:36:06] "GET /static/js/prettify.js HTTP/1.1" 404 2068
[15/Oct/2013 05:36:06] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 2083
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089
[15/Oct/2013 05:36:06] "GET /accounts/login/ HTTP/1.1" 200 3402
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap-responsive.min.css HTTP/1.1" 404 2122
[15/Oct/2013 05:36:19] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089
[15/Oct/2013 05:36:20] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089
I'v read the Django docs and tried several alternative settings but none did help to resolve the problem. So appreciate your hints.
url.spy
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'myproj.views.home', name='home'),
# url(r'^myproj/', include('myproj.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url (r'^accounts/', include('registration.backends.default.urls')),
)
Staticfiles in Django seems to always confuse people who are just starting out, and imo, the documentation could be improved.
Here's a quick breakdown of what's what:
MEDIA_ROOT: The base directory where files you upload as part of a form or model form go. The upload_to property of a File/ImageField is appended to this path.
MEDIA_URL: The URL pattern that files located in MEDIA_ROOT are accessible via.
STATIC_ROOT: The directory that holds static files to be served in production.
STATIC_URL: The URL pattern that files located in STATIC_ROOT are accessible via.
STATICFILES_DIRS: The director(y/ies) that hold static files to be served in development.
When you have the staticfiles app in your INSTALLED_APPS, it will only serve static files located in STATICFILES_DIRS when DEBUG = True.
When you run $ python manage.py collectstatic, it will gather any static directories for your application, and any 3rd party applications and copy them to STATIC_ROOT.
So, to fix your paths to static files in production, make sure your settings are correct and that you've run collectstatic either before you deploy or as part of your deployment process.
There is also a new template tag for static files:
{% load i18n %}
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
...
<link href="{% static 'css/bootstrap.min.css' %}"
rel="stylesheet">
<link href="{% static 'css/bootstrap-responsive.min.css' %}"
rel="stylesheet">
</head>
<body>
...
In your project structure, re-name "/static" to "/static-assets" and set that directory in STATICFILES_DIRS.
Change your STATIC_ROOT to point to "/static", and don't put any directories or files there yourself. Let the collectstatic management command do that.
Here's how I typically structure a project:
/my_app/
/my_app/
__init__.py
settings.py
urls.py
manage.py
/another_app_module/
__init__.py
models.py
...
/static/
/static-assets/
/css
/js
/images
/templates
Given this file structure, and assuming my project sits at: /home/btaylor/django-projects/my_app/ my settings would be:
STATIC_ROOT = '/home/btaylor/django-projects/my_app/static'
STATIC_URL = '/static/'
MEDIA_ROOT = '/home/btaylor/django-projects/my_app/static/uploads'
MEDIA_URL = '/static/uploads/'
STATICFILES_DIRS = ('/home/btaylor/django_projects/my_app/static-assets',)

Django, trouble with STATIC_URL file paths

I have two templates index.html and search_results.html in the same directory.
Both have the following code snippet
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" media="screen" />
The css file loads on the index template:
"GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 304 0
However, does not for the search_results template:
"GET /search/static/bootstrap/css/bootstrap.min.css HTTP/1.1" 404 2133
Since it includes the /search.
I'm wondering how to omit the /search/ portion in trying to load the css file in my second template, since that template all have urls starting with /search/.
my urls.py:
urlpatterns = patterns('',
url(r'^$', 'home.views.index'),
url(r'^search/$', 'home.views.film_chart_view')
)
Clearly, you are missing the slash / at the beginning of the static URL.
The dirty fix:
Change the line in your template to
<link rel="stylesheet" type="text/css" href="/{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" media="screen" />
The clean fix:
Add a slash at the beginning of the STATIC_URL variable in your settings file. It must probably look like this:
STATIC_URL = '/static/'

django is serving static css files, yet somehow js files show a 404

when i try to include js files in my template, as opposed to css files - django can't point the browser to the static folder...
this is the relevant part in my settings.py -
STATIC_ROOT = '/var/www/html/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'static').replace('\\','/'),
)
from within my template, when i'm calling css files as in the following example -
<link rel="stylesheet" type="text/css" href="{% static 'bootstrap/css/bootstrap.css' %}" />
they get loaded just fine -
"GET /static/bootstrap/css/bootstrap.css HTTP/1.1" 200 127343
yet, when i try to load js files like so -
<script type="text/javascript" src="{% static '/bootstrap/js/jquery.js' %}"></script>
the request gets broken, and in the runserver terminal window i get errors like -
"GET /bootstrap/js/jquery.js HTTP/1.1" 404 2848
showing that the 'static' part of the url gets dropped out...
anyone has an idea as for why this is happening? thanks a lot everyone!
I usually use:
<a href="{{ STATIC_URL }}path/to/file">
So:
<script type="text/javascript" src="{{ STATIC_URL }}bootstrap/js/jquery.js"></script>
and this works for js files fine. So maybe try this as opposed to the {% static %} tag to see if it works.