LoginSignup
0
1

More than 3 years have passed since last update.

Python Django CSS 反映

Last updated at Posted at 2020-04-21

前提

css反映のさせかたが曖昧だったので記述します。

【環境】
os
python3.7.3
Django3.0.5

アプリケーション名:sns

【ファイル構成】
sns
| - snsproject
| | - setting.py
| | - urls.py
| - snsapp
| | - urls.py
| | - views.py
| - templates
| | - sns
| | | - index.html
| - static
| | - css
| | | - style.css
|manage.py

本題

早速実装していきます。

settings.py
・・・
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sns', #追記
]
・・・
TEMPLATES = [
    {
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'), #追記
        ],
            ],
        },
    },
]
・・・

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    [
        os.path.join(BASE_DIR, "static"), #追記
    ]
)

続いてtemplates/index.htmlを編集していきます。

index.html
{% 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>sns</title>
    <link href="{% static 'css/stylesheet.css' %}" rel="stylesheet">
  </head>
  <body>
  </body>
</html>

これでCSSが反映されるようになりました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1