Here's how to reflect CSS in Django.
Python 3.7.6 Django 3.0.5
Write the following code at the bottom of setting.py.
setting.py
#Upper part omitted
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
[
os.path.join(BASE_DIR, "static"),
]
)
Add a static folder and make it as follows
| - webproject
| | - setting.py
| | - urls.py
| - webapp
| | - urls.py
| | - views.py
| - templates
| | - form.html
| - static
| | - css
| | | - style.css
|manage.py
Add ``` {% load static%}` `` at the top.
{% load static %}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WebApp</title>
<link href="{% static 'css/style.css' %}" rel="stylesheet">
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Make sure it is reflected.
p {
color: blue;
}
Recommended Posts