※弊社記事はたぶんにPRが含まれますので
話半分で受け取ってください。

Django公式サイトのチュートリアルを咀嚼しながらやってみる

7. はじめての Django アプリ作成、その 6

出典:はじめての Django アプリ作成、その 6 | Django ドキュメント | Django
https://docs.djangoproject.com/ja/3.2/intro/tutorial06/

7.1. アプリの構造をカスタマイズする

 テンプレートのファイルと同様、polls 使用する静的ファイルは polls/static/polls ディレクトリ下に置きます。

 スタイルシートを使ってリンクの色を変えます。

li a {
	color: green;
}

 index.html にCSSを読み込むためのコードを追加します。

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

 サーバを起動して http://localhost:8000/polls/ にアクセスします。

% python manage.py runserver

 リンクの色が緑になっているはず。

7.2. 背景画像を追加する

 polls/static/polls ディレクトリ下に images ディレクトリを作成しその中に背景用の画像 background.gif を置きます。

 スタイルシートで背景画像を設定します。

body {
    background: white url("images/background.gif") no-repeat;
}

ページ: 1 2 3 4 5 6 7 8

関連する記事