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
Related
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!
I am struggling with django static files. I want to link "screen.css" stored in "all_statics/css" folder to "index.html".
Here is my html file :
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Gwendolyn:wght#700&display=swap" rel="stylesheet">
<link href="{% static '/css/screen.css' %}" rel="stylesheet" >
</head>
<body>
<h1>Test</h1>
</body>
</html>
In my settings.py, I have set these variables :
STATIC_URL = 'static/'
STATICFILES_DIR = [
BASE_DIR / 'all_statics',
]
STATIC_ROOT = BASE_DIR / 'static'
Here is my project structure :
Project structure
When I run python manage.py collectstatic , the static files from admin app is copied to static folder, but not the files from 'all_statics' .
When I run python manage.py runserver and browse to the index page, I see that the css can't be found (404) :
[18/Jan/2022 11:30:26] "GET / HTTP/1.1" 200 308
[18/Jan/2022 11:30:27] "GET /static/css/screen.css HTTP/1.1" 404 1801
When I run python .\manage.py findstatic css/screen.css I get : No matching file found for 'css/screen.css'.
I have created a sample project to show my problem. You can clone it from https://github.com/doolieSoft/django_project.git .
Do you have an idea why my css can't be found ?
Thanks for your help !
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
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/'
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.