1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Django でとりあえずstatic参照するメモ

Posted at

概要

前提

  • Ubuntu 20.04.1 LTS on WSL2
  • Python 3.9.0
  • Django 3.1.2

ディレクトリ構成

プロジェクト直下の static/css/style.css を参照します

myproject
├── manage.py
├── myproject
│   ├── settings.py
└── static
    └── css
        └── style.css

設定

INSTALLED_APPSSTATIC_URL はデフォルトで設定されていると思います

settings.py
INSTALLED_APPS = [
...(snip)
    'django.contrib.staticfiles',   # デフォルトで設定されている
...(snip)
]

STATIC_URL = '/static/'   # デフォルトで設定されている

# 追加する部分
STATICFILES_DIRS = [
    BASE_DIR.joinpath('static'),
]

確認

myproject/static 内が参照対象となっています

$ python manage.py findstatic css/style.css
Found 'css/style.css' here:
  /home/user/myproject/static/css/style.css

テンプレートの記述

templates/index.html
{% load static %}

...(snip)
<link rel="stylesheet" href="{% static "css/style.css" %}">
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?