30
loading...
This website collects cookies to deliver better user experience
static
as the name of the folder. So, if you have created the template folder in the root directory, similar to that static folder can be created in that path. settings.py
file to actually tell Django web server to look for all our static files in that folder. To do that, go to the settings.py
file, now by this time you would have known where the settings.py
file is (inside the project-named folder). Add the following at the end of the settings.py
file.# import os
# STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static/"),
)
import os
if you already have imported and the STATIC_URL
if already there in the file. The STATICFILES_DIRS
is the configuration that we tell the django environment to look for all our static files in the base/root directory of the project where the static/
folder is. The os.path.join()
actually gets the path of the directory in ourr operating system to the folder specified in the case of our project the BASE_DIR
is the path of the project and we add in the static folder to actually the project path. The final piece and the crucial one is the "static/"
path, this can be other location where you have created your static folder within the project.css
, js
and assets
(or img
) mostly. And inside of this folders you can store the respective static files. This also creates the project more scalable in terms of it's maintenance.static\
|__css\
|__js\
|__assets\
body
{
background-color:#1d1dff;
color:white;
}
h1
{
text-align:center
font-family: monospace;
}
p
{
color:#ff6600;
font-weight:500;
}
ul
{
list-style-type:square;
}
STATIC_ROOT
which the server then loads, we'll see how that internally works when we get to deployment techniques for Django project. {% load static %}
static
tag which allows us to embed the links to the static files as explained earlier. <link rel="stylesheet" href="{% static 'css/style.css' %}">
{% static 'path-to-file' %}
post
in your django project, you can render static files as below:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Blog</title>
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<h1>Hello, World!</h1>
{% block body %}
<p>This is not going to get inherited </p>
{% endblock %}
<p>This will be inherited</p>
</body>
</html>
body
{
background-color:#1d1dff;
color:white;
}
h1
{
text-align:center
font-family: monospace;
}
p
{
color:#ff6600;
font-weight:500;
}
ul
{
list-style-type:square;
}
style.css
stored inside the css folder of the static folder. This contains basic (very lame) CSS styling as we can understand. from django.shortcuts import render
def home(request):
return render(request, 'home.html')
views.py
file has the function that renders the template home.html
from the templates folder inside the application specific folder. from django.urls import path
from post import views
urlpatterns = [
path('', views.home, name="home"),
]
views.py
file. The url in this file(code-snippet) is linking the root url('') to the home view in the views.py
file.from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('post.urls')),
]
import os
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static/"),
)
for.html
template created in the previous part. {% extends 'home.html' %}
{% load static %}
{% block body %}
<img src="{% static 'assets/tbicon.png' %}" height="50px" width="50px" />
<ul>
{% for sport in sport_list %}
<li>{{ sport }}</li>
{% endfor %}
</ul>
{% endblock %}
{% load static %}
again as we are loading the static file (image) in this template.from django.shortcuts import render
def for_demo(request):
sports = ('football', 'cricket', 'volleyball', 'hockey', 'basketball')
return render(request, 'for.html', {'sport_list': sports})
def home(request):
return render(request, 'home.html')
from django.urls import path
from post import views
urlpatterns = [
path('', views.home, name="home"),
path('for/', views.for_demo, name="fordemo"),
]
127.0.0.1:8000/for/
url to see the below result: