質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.39%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Q&A

解決済

1回答

780閲覧

DjangoのテンプレートでstaticディレクトリのCSSが適用されない

luke04

総合スコア7

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

0グッド

0クリップ

投稿2021/07/31 10:25

DjangoでWebアプリを開発しているのですが、staticディレクトリにあるCSSファイルが適用されません。urlから直接ファイルにアクセスするとちゃんとCSSのコードが表示されます。また、同様にstaticにある画像はちゃんと読み込まれます。原因がわかる方、いらっしゃいますでしょうか。

ディレクトリ階層

.
├── SFL
│   ├── init.py
│   ├── pycache(以下略)
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── accounts
│   ├── init.py
│   ├── pycache(以下略)
│   ├── admin.py
│   ├── apps.py
│   ├── migrations(以下略)
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── media
│   └── dora.png
├── post
│   ├── init.py
│   ├── pycache(以下略)
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations(以下略)
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── static
│   ├── accounts
│   │   └── top_page.css**(問題のCSS)**
│   └── images
│   ├── dictionary.jpg
│   ├── read.jpg
│   ├── service-header.jpg
│   ├── talk.jpg
│   ├── user_icon.png
│   └── write.jpg
└── templates
| ├── accounts
| │   ├── top_page.html**(問題のhtml)**
| │   └── user_profile.html
| ├── base.html
| └── post
| ├── post_compose.html
| └── post_list.html

関係ありそうなコード

static/settings.py

SFL/settings.py

1""" 2Django settings for SFL project. 3 4Generated by 'django-admin startproject' using Django 3.2.5. 5 6For more information on this file, see 7https://docs.djangoproject.com/en/3.2/topics/settings/ 8 9For the full list of settings and their values, see 10https://docs.djangoproject.com/en/3.2/ref/settings/ 11""" 12 13from pathlib import Path 14import os 15 16# Build paths inside the project like this: BASE_DIR / 'subdir'. 17BASE_DIR = Path(__file__).resolve().parent.parent 18 19 20# Quick-start development settings - unsuitable for production 21# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ 22 23# SECURITY WARNING: keep the secret key used in production secret! 24SECRET_KEY = 'django-insecure-&t%!m8o8!*j6!v=9lbi)z)g72bjvw1-au+&6vn-#1&@k^u!@h)' 25 26# SECURITY WARNING: don't run with debug turned on in production! 27DEBUG = True 28 29ALLOWED_HOSTS = [] 30 31 32# Application definition 33 34INSTALLED_APPS = [ 35 'django.contrib.admin', 36 'django.contrib.auth', 37 'django.contrib.contenttypes', 38 'django.contrib.sessions', 39 'django.contrib.messages', 40 'django.contrib.staticfiles', 41 42 'post.apps.PostConfig', 43 'accounts.apps.AccountsConfig', 44] 45 46MIDDLEWARE = [ 47 'django.middleware.security.SecurityMiddleware', 48 'django.contrib.sessions.middleware.SessionMiddleware', 49 'django.middleware.common.CommonMiddleware', 50 'django.middleware.csrf.CsrfViewMiddleware', 51 'django.contrib.auth.middleware.AuthenticationMiddleware', 52 'django.contrib.messages.middleware.MessageMiddleware', 53 'django.middleware.clickjacking.XFrameOptionsMiddleware', 54] 55 56ROOT_URLCONF = 'SFL.urls' 57 58TEMPLATES = [ 59 { 60 'BACKEND': 'django.template.backends.django.DjangoTemplates', 61 'DIRS': [os.path.join(BASE_DIR, 'templates')], 62 'APP_DIRS': True, 63 'OPTIONS': { 64 'context_processors': [ 65 'django.template.context_processors.debug', 66 'django.template.context_processors.request', 67 'django.contrib.auth.context_processors.auth', 68 'django.contrib.messages.context_processors.messages', 69 ], 70 }, 71 }, 72] 73 74WSGI_APPLICATION = 'SFL.wsgi.application' 75 76 77# Database 78# https://docs.djangoproject.com/en/3.2/ref/settings/#databases 79 80DATABASES = { 81 'default': { 82 'ENGINE': 'django.db.backends.postgresql_psycopg2', 83 'NAME': 'django_boards', 84 'USER': 'lukas', 85 'PASSWORD': 'newpassword', 86 'HOST': 'localhost', 87 'PORT': 5432, 88 } 89} 90 91 92# Password validation 93# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators 94 95AUTH_PASSWORD_VALIDATORS = [ 96 { 97 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 98 }, 99 { 100 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 101 }, 102 { 103 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 104 }, 105 { 106 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 107 }, 108] 109 110 111# Internationalization 112# https://docs.djangoproject.com/en/3.2/topics/i18n/ 113 114LANGUAGE_CODE = 'ja' 115 116TIME_ZONE = 'Asia/Tokyo' 117 118USE_I18N = True 119 120USE_L10N = True 121 122USE_TZ = True 123 124 125# Static files (CSS, JavaScript, Images) 126# https://docs.djangoproject.com/en/3.2/howto/static-files/ 127 128STATIC_URL = '/static/' 129STATICFILES_DIRS = [ 130 os.path.join(BASE_DIR, 'static') 131] 132 133# Default primary key field type 134# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field 135 136DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 137 138AUTH_USER_MODEL = 'accounts.User' 139 140MEDIA_URL = '/media/' 141MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

問題のtemplete html

{% extends 'base.html' %} {% load static %} {% block link %} <link rel="stylesheet" href="https://unpkg.com/ress/dist/ress.min.css"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Caveat:wght@700&display=swap"> <link rel="stylesheet" href="{% static 'accounts/top_page.css' %}"> {% endblock link %} {% block content %} <header> <div class="page-header wrapper"> <h1 class="header-logo">SFL</h1> <nav> <ul class="header-nav"> <li><a href="">ログイン</a></li> <li><a href="">新規登録</a></li> </ul> </nav> </div> </header> <div class="top-content"> <div class="service-header-img"> <div class="service-header wrapper"> <h2>Study with Foreign Languages!</h2> <div>SFLは異国語を使って色々な教科の勉強ができるサービスです。</div> </div> </div> <div class="service-detail wrapper"> <div class="service"> <img src="{% static 'images/write.jpg' %}"> <h3>Write</h3> <p>学ぶ内容を投稿することで世界中の人に見てもらう</p> </div> <div class="service"> <img src="{% static 'images/read.jpg' %}"> <h3>Read</h3> <p>世界中の人の投稿から学ぶ</p> </div> <div class="service"> <img src="{% static 'images/talk.jpg' %}"> <h3>Talk</h3> <p>世界中の人と議論を交わす</p> </div> <div class="service"> <img src="{% static 'images/talk.jpg' %}"> <h3>Search</h3> <p>専門用語に特化した辞書機能を開発中</p> </div> </div> </div> <footer> <div class="wrapper"> <p><small>&copy; 2021 Lukas</small></p> </div> </footer> {% endblock content %}

###問題のCSS

@charset "UTF-8"; html { font-size: 100%; } body { font-family: "Yu Gothic Medium", "游ゴシック Medium", "YuGothic", "游ゴシック体", "ヒラギノ角ゴ Pro W3", "sans-serif"; line-height: 1.7; color: #001279; } a { text-decoration: none; } .wrapper { max-width: 1200px; margin: 0 auto; padding: 0 4%; } header { background-color: #00fff2; } .page-header { display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-pack: justify; -ms-flex-pack: justify; justify-content: space-between; } .header-logo { font-size: 50px; } .header-nav { display: -webkit-box; display: -ms-flexbox; display: flex; font-size: 1.25rem; margin: 35px 0; list-style: none; } .header-nav li { margin-left: 36px; } .header-nav a:hover { color: #50c5fc; } .service-header { color: #fff; text-align: center; height: 600px; } .service-header h2 { font-size: 100px; padding-top: 100px; font-family: 'Caveat', cursive; text-shadow: 2px 2px 10px #229cff, -2px 2px 10px #229cff, 2px -2px 10px #229cff, -2px -2px 10px #229cff; } .service-header div { padding-top: 100px; font-size: 1.5rem; color: #001279; } .service-header-img { background-image: -webkit-gradient(linear, left top, left bottom, from(#00fff2), to(white)); background-image: linear-gradient(#00fff2, white); } .service-detail { display: -ms-grid; display: grid; gap: 50px; -ms-grid-columns: 1fr 1fr; grid-template-columns: 1fr 1fr; margin-top: 6%; margin-bottom: 50px; } .service { text-align: center; border-radius: 10px; background-color: #d5fffd; } .service img { margin-top: 30px; width: 400px; } footer { color: #fff; height: 100px; background-color: #000; text-align: center; } footer p { font-size: 1.125rem; padding: 30px 0; } /*# sourceMappingURL=style.css.map */

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

単純なミスで、base.html<head>~</head>内のblock名とtop_page.html内のそれとが一致していないだけでした。

投稿2021/07/31 10:33

luke04

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.39%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問