So I've seen a lot of tutorials here explaining what to do, but no matter what I change the static files won't seem to load. This is what my files are looking like atm
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(PROJECT_ROOT, "static"),'']
job_app/urls.py
urlpatterns = [
path('', main, name='index'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>JobPortal - Free Bootstrap 4 Template by Colorlib</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% load static %}
<link href="https://fonts.googleapis.com/css?family=Nunito+Sans:200,300,400,600,700,800,900" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/open-iconic-bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/animate.css' %}">
<link rel="stylesheet" href="{% static 'css/owl.carousel.min.css' %}">
<link rel="stylesheet" href="{% static 'css/owl.theme.default.min.css' %}">
<link rel="stylesheet" href="{% static 'css/magnific-popup.css' %}">
<link rel="stylesheet" href="{% static 'css/aos.css' %}">
<link rel="stylesheet" href="{% static 'css/ionicons.min.css' %}">
<link rel="stylesheet" href="{% static 'css/bootstrap-datepicker.css' %}">
<link rel="stylesheet" href="{% static 'css/jquery.timepicker.css' %}">
<link rel="stylesheet" href="{% static 'css/flaticon.css' %}">
<link rel="stylesheet" href="{% static 'css/icomoon.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
jobber/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('job_app.urls'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Below configs worked for me. Django only needs two things.
What should be the base static URL (STATIC_URL)
And where to search static files (STATICFILES_DIRS)
Remove static urlpatterns from urls.py. Only define STATIC_URL and STATICFILES_DIRS in settings.py and remove STATIC_ROOT.
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Related
I have downloaded an html template online. And I have gone ahead and pasted the index.html to my templates folder, and the assets to my static files, and I have placed the '{% load static %)' tag inside my index file.
Here is a snippet of the index.html
{% load static %}
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>ClassiGrids - Classified Ads and Listing Website Template.</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" type="image/x-icon" href="{% static 'assets/images/favicon.svg' %}" />
<!-- Place favicon.ico in the root directory -->
<!-- Web Font -->
<link
href="https://fonts.googleapis.com/css2?family=Jost:ital,wght#0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap"
rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Lato&display=swap" rel="stylesheet">
<!-- ========================= CSS here ========================= -->
<link rel="stylesheet" href="{% static 'assets/css/bootstrap.min.css' %}" />
<link rel="stylesheet" href="{% static 'assets/css/LineIcons.2.0.css' %}" />
<link rel="stylesheet" href="{% static 'assets/css/animate.css' %}" />
<link rel="stylesheet" href="{% static 'assets/css/tiny-slider.css' %}" />
<link rel="stylesheet" href="{% static 'assets/css/glightbox.min.css' %}" />
<link rel="stylesheet" href="{% static 'assets/css/main.css' %}" />
</head>
<body>
<!--[if lte IE 9]>
<p class="browserupgrade">
You are using an <strong>outdated</strong> browser. Please
upgrade your browser to improve
your experience and security.
</p>
<![endif]-->
<!-- Preloader -->
<div class="preloader">
<div class="preloader-inner">
<div class="preloader-icon">
<span></span>
<span></span>
</div>
</div>
</div>
<!-- /End Preloader -->
I have also edited the settings to show where my template files are located
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR/ 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
However, the site is still appearing in plain html on the browser. What could I have missed?
It appears that I forgot to edit the settings to include the static files, by importing the operating system
from pathlib import Path
import os
and adding the static files directory at the bottom
STATIC_URL = 'static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
jquery does not load - Page not found (404)
base.html
{% load static %}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="{% static 'bootstrap-5.0.0/css/bootstrap.css' %}" rel="stylesheet">
<link href="{% static 'css/style.css' %}" rel="stylesheet">
<!-- jQuery -->
<script src="{% static 'jquery/jquery.js' %}"></script>
</head>
setting.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'ServiceCRM3/static'),]
static folder created
the script is in the path static / jquery / jquery.js
bootstrap picks up ok in the same folder
the server restarted
what's wrong?
Hello i can't resolve static file (vendor,bootstrap,css,js) i can t see the picture with color just baisc formular need help to resolve this i followed the documentation step by step but this step didnt work
this is my code html :
<!DOCTYPE html>
<html lang="en">
<head>
{% load static %}
<title>Login V9</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
<link rel="icon" type="image/png" href="{{ static
'AppStage/images/icons/favicon.ico' }}"/>
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/vendor/bootstrap/css/bootstrap.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/fonts/font-awesome-4.7.0/css/font-awesome.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/fonts/iconic/css/material-design-iconic-font.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/vendor/animate/animate.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/vendor/css-hamburgers/hamburgers.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/vendor/animsition/css/animsition.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/vendor/select2/select2.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/vendor/daterangepicker/daterangepicker.css' %} ">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/css/util.css' %} ">
<link rel="stylesheet" type="text/css" href="{% static
'AppStage/css/main.css' %} ">
<!--===============================================================================================-->
</head>
<body>
<div class="container-login100" style="background-image: url('images/paul.jpg');">
<div class="wrap-login100 p-l-55 p-r-55 p-t-80 p-b-30">
<form class="login100-form validate-form">
<span class="login100-form-title p-b-37">
Sign In
</span>
<div class="wrap-input100 validate-input m-b-20" data-validate="Enter username or email">
<input class="input100" type="text" name="username" placeholder="matricule">
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input m-b-25" data-validate = "Enter password">
<input class="input100" type="password" name="pass" placeholder="password">
<span class="focus-input100"></span>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn">
Sign In
</button>
</div>
</form>
</div>
</div>
<div id="dropDownSelect1"></div>
<!--===============================================================================================-->
<script src="{% static
'AppStage/vendor/jquery/jquery-3.2.1.min.js' %}"></script>
<script src="{% static
'AppStage/vendor/animsition/js/animsition.min.js' %}"></script>
<script src="{% static
'AppStage/vendor/bootstrap/js/popper.js' %}"></script>
<script src="{% static
'AppStage/vendor/bootstrap/js/bootstrap.min.js' %}"></script>
<!--===============================================================================================-->
<script src="{% static
'AppStage/vendor/select2/select2.min.js' %}"></script>
<!--===============================================================================================-->
<script src="{% static
'AppStage/vendor/daterangepicker/moment.min.js' %}"></script>
<script src="{% static
'AppStage/vendor/daterangepicker/daterangepicker.js' %}"></script>
<!--===============================================================================================-->
<script src="{% static
'AppStage/vendor/countdowntime/countdowntime.js' %}"></script>
</body>
</html>
this is the file views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
# Create your views here.
def Authenti(request):
template = loader.get_template('authentification/index.html')
return HttpResponse(template.render(request=request))
the picture of hearchi of my project
[1]: https://zupimages.net/up/19/29/dkik.png"tooltip"
first link is wrong
<link rel="icon" type="image/png" href="{% static 'AppStage/images/icons/favicon.ico' %}"/>
use path for image,
.container-login100 {
background: url("/AppStage/images/paul.jpg")
}
I created added a dashboard to my project it is using bootstrap files which I am trying to load but aren't loading.
I am trying to load static file using {% static '' %} and it's now picking up the files. You can ask for further code.
Here is the code of the template
<link href=" {%static '../static/Dashboard/assets/css/bootstrap.min.css'%}" rel="stylesheet" />
<link href="{%static '../static/Dashboard/assets/css/bootstrap.min.css '%}" rel="stylesheet"/>
<link href="{% static '../static/Dashboard/assets/css/paper-dashboard.css' %}" rel="stylesheet"/>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Muli:400,300' rel='stylesheet' type='text/css'>
<link href=" {%static '../static/Dashboard/assets/css/themify-icons.css' %}" rel="stylesheet">
Setting in setting.py
STATICFILES_DIR=[
os.path.join(BASE_DIR, "static"),
'final_project/static',
]
STATIC_URL = '/static/'
In main folder of your project, create a folder named static. then in this folder create two other folders, named static_root and static_dirs
now change the settings.py file like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static', 'static_dirs'),
)
then add static files in static/static_dirs directory.
now you can use static files in your templates like this:
<link href="{% static 'Dashboard/assets/css/paper-dashboard.css' %}" rel="stylesheet"/>
your template should be like this:
<!DOCTYPE html>
{% load staticfiles %}
<head>
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
</head>
</html>
css/bootstrap.min.css is in final_project/static directory
Do not forget add {% load staticfiles %}
in settings.py you define static path, then this way is incorrect:
<link href="{% static '../static/Dashboard/assets/css/paper-dashboard.css' %}" rel="stylesheet"/>
correct:
<link href="{% static 'Dashboard/assets/css/paper-dashboard.css' %}" rel="stylesheet"/>
I'm developing with django first time. I have read what is : STATIC_ROOT / STATICFILES_DIR / STATIC_URL, I know what purpose for each of them.
I have a problem which I keep getting 404 on browser trying to get the css files.
setting.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = '%s/online_static/' % (BASE_DIR)
STATICFILES_DIRS = ['%s/bootstrap/' % (PROJECT_DIR),]
index.html
<head>
{% load staticfiles %}
<meta charset="UTF-8">
<title>{% block title%}Default title{% endblock title %}</title>
<meta name="description" content="{% block metadescription%}{% endblock metadescription %}">
<meta name="keywords" content="{% block metakeywords%}{% endblock metakeywords %}">
<meta name="author" content="alme7airbi9357#gmail.com">
<!-- Bootstrap core CSS -->
<link href="{% static 'vendor/bootstrap/css/bootstrap.min.css' %}" type="text/css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="{% static 'css/shop-homepage.css' %}" rel="stylesheet" type="text/css">
<!-- Bootstrap core JavaScript -->
<script src="{% static 'vendor/jquery/jquery.min.js' %}"></script>
<script src="{% static 'vendor/bootstrap/js/bootstrap.bundle.min.js' %}"></script>
</head>
what I did
after setting this params I have run the python manage.py collectstatic
Note : DEBUG = False
As per the Django Docs, follow the configuration steps for static : https://docs.djangoproject.com/en/1.11/howto/static-files/