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 !
Related
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
I'm trying to load in bootstrap into my html page but I get a 404 status code in the network tab in the developers tools saying that it could not find the file
the request url is
http://127.0.0.1:8000/static/css/bootstrap.css
this is my html page where I am trying to use to the file
<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dog Groomers</title>
<link rel="stylesheet" type="text/css" href="{% static '/css/bootstrap.css' %}"/>
</head>
</html>
Here is my file structure
in my settings.py file
STATIC_URL = 'static/'
STATICFILES_DIR = [os.path.join(BASE_DIR, 'static')]
#edit wasn't included before in question
DEBUG = TRUE
Do I need to have a static folder in every folder instead of just having it in the root?
Kindly update your settings.py
You are using this:
STATICFILES_DIR = [os.path.join(BASE_DIR, 'static')]
Try this one, it should work!
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I want to add a static file to my django project app.
My app is named "core"
Hence inside the app where I need the static file (called main.css) , I made a directory named static/core/main.css
So after that my directory looks like this
core
....
-static
|_core
|_main.css
.........
And in the settings.py file , I wrote this
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
And in the html file where I want the static css I wrote this
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %} Welcome | Ecommerce {% endblock %}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma#0.9.1/css/bulma.min.css">
<link rel="stylesheet" href="{% static 'core/main.css' %}">
This HTML file is located in a global project level template folder where I dump the templates from all the apps in the project .
But my static file is not getting loaded for some reason
May be because you didn't specify the file?
I guess you should change it from
<link rel="stylesheet" href="{% static '' %}">
To
<link rel="stylesheet" href="{% static 'core/main.css' %}">
Try to add
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
into your Django settings
In Your settings.py file add this
STATIC_URL = '/static/'
if DEBUG:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
after that open the terminal in your root directory and run the following command
python manage.py collectstatic
you can also refer to the django documentation where they have explained about staticfiles in django https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/
My website favicon icons are no longer found. It is a simple Django website with bootstrap. It was working a few minutes ago, I tried changing the whole boot strap code.
The log i am getting when the django server is running and I use the users profile:
Not Found: /favicon.ico
[06/Feb/2021 08:53:13] "GET /favicon.ico HTTP/1.1" 404 4502
Session data corrupted
This is were the bootstrap static is coming though in the page:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css"
href="{% static 'blog/main.css' %}">
Most browsers look for the existence of an file called favicon.ico at the root path of your website domain, this controls the icon for the website you can see in your bookmarks folder or the address bar of your browser.
If you don't have one, then it's valid that it would return a Not Found error.
When you deploy to something like Apache, you will have to alias your favicon in a config file. However, while running Django in development mode, the following works
urls.py
from django.views.generic import RedirectView
from django.urls import path
urlpatterns=[
...
path('favicon.ico',RedirectView.as_view(url='/static/images/favicon.ico')),
]
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.