PythonDjango

Djangoで作る家計簿アプリ ②支出一覧ページの作成

公開日:2021-10-12 更新日:2023-06-08

Djangoで作る家計簿アプリシリーズの二つ目の記事です。

今回はトップページと簡単な支出一覧ページを作成していきます。

ベーステンプレートの作成

先にベーステンプレートを作っておきましょう。

アプリ内にtemplateskakeiboとディレクトリを作成し、base.htmlファイルを作ります。

<!-- kakeibo/templates/kakeibo/base.html -->
{% load static %}
<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>家計簿アプリ</title>
  <link rel="stylesheet" href="{% static 'kakeibo/css/reset.css' %}">
  <link rel="stylesheet" href="{% static 'kakeibo/css/style.css' %}">
  {% block extrajs %}{% endblock %}
</head>

<body>
  <header class="page-header">
    <h1>
      <a href="{% url 'kakeibo:payment_list' %}" class="header-title">家計簿アプリ</a>
    </h1>
    <nav class="nav">
      <ul class="main-nav ml-5">
        <li class="ml-5">
          <a href="{% url 'kakeibo:payment_list'%}">支出一覧</a>
        </li>
    </nav>
  </header>

  <div class="layout">
    <div class="container">
      <main>
        {% block content %}{% endblock %}
      </main>
    </div>
  </div>

</body>

</html>

今回はreset.cssを読み込み、cssを初期化してから装飾をしていきます。

アプリ内にstatickakeibocssとディレクトリを作り、reset.cssとstyle.cssを作成します。

/* kakeibo/static/kakeibo/css/reset.css */

/**
 * html5doctor.com Reset Stylesheet v1.6.1 (http://html5doctor.com/html-5-reset-stylesheet/)
 * Richard Clark (http://richclarkdesign.com)
 * http://cssreset.com
 */
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
    margin:0;
    padding:0;
    border:0;
    outline:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}
body {
    line-height:1;
}
article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section {
    display:block;
}
nav ul {
    list-style:none;
}
blockquote, q {
    quotes:none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content:'';
    content:none;
}
a {
    margin:0;
    padding:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}
/* change colours to suit your needs */
ins {
    background-color:#ff9;
    color:#000;
    text-decoration:none;
}
/* change colours to suit your needs */
mark {
    background-color:#ff9;
    color:#000;
    font-style:italic;
    font-weight:bold;
}
del {
    text-decoration: line-through;
}
abbr[title], dfn[title] {
    border-bottom:1px dotted;
    cursor:help;
}
table {
    border-collapse:collapse;
    border-spacing:0;
}
/* change border colour to suit your needs */
hr {
    display:block;
    height:1px;
    border:0;
    border-top:1px solid #cccccc;
    margin:1em 0;
    padding:0;
}
input, select {
    vertical-align:middle;
}

/* kakeibo/static/kakeibo/css/style.css */

@charset "UTF-8";

/* --------------------------------
 * base
 * -------------------------------- */
html {
  font-size: 62.5%;
}

*, *::before, *::after {
  box-sizing: border-box;
}

body {
  line-height: 1;
  background-color: #fff;
  color: #333;
  font-size: 1.4rem;
  font-family: "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
}

.layout {
  padding-top: 56px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

@media (min-width: 768px) {
  body {
    font-size: 16px;
  }
}

a {
  text-decoration: none;
  color: #00809d;
}

a:hover {
  opacity: .7;
} 

/* --------------------------------
 * レイアウト・コンテナ
 * -------------------------------- */
.layout {
  padding-top: 56px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main {
  padding: 36px 0;
  min-height: 40vh;
}

.container {
  margin: 0 10px;
  position: relative;
}

@media (min-width: 768px) {
  .container {
    margin: 0 40px;
  }
}

@media (min-width: 1024px) {
  .container {
    width: 960px;
    margin: 0 auto;
  }
}

/* --------------------------------
 * ヘッダー
 * -------------------------------- */
.page-header {
  padding: 30px 4% 10px;
  position: fixed;
  top: 0;
  width: 100%;
  background-color: #fafafa;
  display: flex;
  align-items: baseline;
  z-index: 9999;
}

a.header-title {
  font-size: 2rem;
  margin: 0;
  padding: 0;
  color:#888;
}

.main-nav {
  list-style: none;
  margin: 0;
  display: flex;
}

.main-nav li {
  margin: 0 0 0 10px;
  font-size: 14px;
}

.main-nav a {
  color: #888;
}

.main-nav a:hover {
  opacity: 0.6;
}

/* --------------------------------
 * ユーティリティ
 * -------------------------------- */
.mt-0 {
  margin-top: 0 !important;
}

.mt-1 {
  margin-top: 0.25rem !important;
}

.mt-2 {
  margin-top: 0.5rem !important;
}

.mt-3 {
  margin-top: 1rem !important;
}

.mt-4 {
  margin-top: 1.5rem !important;
}

.mt-5 {
  margin-top: 3rem !important;
}

.mb-0 {
  margin-bottom: 0 !important;
}

.mb-1 {
  margin-bottom: 0.25rem !important;
}

.mb-2 {
  margin-bottom: 0.5rem !important;
}

.mb-3 {
  margin-bottom: 1rem !important;
}

.mb-4 {
  margin-bottom: 1.5rem !important;
}

.mb-5 {
  margin-bottom: 3rem !important;
}

.ml-0 {
  margin-left: 0 !important;
}

.ml-1 {
  margin-left: 0.25rem !important;
}

.ml-2 {
  margin-left: 0.5rem !important;
}

.ml-3 {
  margin-left: 1rem !important;
}

.ml-4 {
  margin-left: 1.5rem !important;
}

.ml-5 {
  margin-left: 3rem !important;
}

.mr-0 {
  margin-right: 0 !important;
}

.mr-1 {
  margin-right: 0.25rem !important;
}

.mr-2 {
  margin-right: 0.5rem !important;
}

.mr-3 {
  margin-right: 1rem !important;
}

.mr-4 {
  margin-right: 1.5rem !important;
}

.mr-5 {
  margin-right: 3rem !important;
}

ユーティリティの部分はBootstrapで使われているマージンの指定スタイルです。

今回はBootstrap自体は使わないですが、コンポーネントのマージン取りが非常に簡便になるので導入しています。

URLパターン、ビューの編集

まず、プロジェクトのurls.pyです。

# project/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('kakeibo.urls')),
]

次にkakeiboアプリ内にurls.pyを作成します。

# kakeibo/urls.py
from django.urls import path
from . import views

app_name = 'kakeibo'

urlpatterns = [
    path('', views.PaymentList.as_view(), name='payment_list'),
]

views.pyを編集します。

#kakeibo/views.py
from django.views import generic
from .models import Payment, PaymentCategory, Income, IncomeCategory


class PaymentList(generic.ListView):
    template_name = 'kakeibo/payment_list.html'
    model = Payment
    ordering = '-date'

作成したモデルはいずれ使うのですべてimportしておきましょう。

payment_list.htmlを作成します。

<!-- kakeibo/templates/kakeibo/payment_list.html -->
{% extends 'kakeibo/base.html' %}
{% block content %}

<form class="mt-2" id="search-form" action="" method="GET">
  <h1>検索フォーム!</h1>
</form>

<table class="table mt-3">
  <tr>
    <th>日付</th>
    <th>カテゴリ</th>
    <th>金額</th>
    <th>摘要</th>
  </tr>
  {% for payment in payment_list %}

  <tr>
    <td>{{ payment.date }}</td>
    <td>{{ payment.category }}</td>
    <td>{{ payment.price}}</td>
    <td>
      {% if payment.description %}
      {{ payment.description }}
      {% endif %}
    </td>
  </tr>
  {% endfor %}
</table>

<div class="mt-5">
  <h1>ページネーション!</h1>
</div>

{% endblock %}

ページ上部に検索フォーム、下部にページネーションを配置しています。

これから作りこみをしていく部分です。

テーブルのスタイルを追加しておきましょう。

/* kakeibo/static/kakeibo/css/style.css */

...省略

/* --------------------------------
 * テーブル
 * -------------------------------- */
.table {
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  font-size: 1.4rem;
}

.table tr {
  border-bottom: dotted 0.5px #432c02;
}

.table tr:hover {
  background-color: #fafafa;
}

.table th, .table td {
  text-align: center;
  width: 20%;
  padding: 15px 0;
}

いくつか支出を登録してみると良いでしょう。

このような見た目になります。

金額をカンマ区切りにする

細かい点ですが、金額は3桁ごとにカンマを打つようにしましょう。

これはDjangoの組み込みのhumanizeを使うと簡単です。

まず、settings.pyを以下のようにします。

#project/settings.py
...

INSTALLED_APPS = [
    ...
    'django.contrib.humanize',  # add
]

....
# add
NUMBER_GROUPING = 3

あとはテンプレート内で読み込むだけです。

{% extends 'kakeibo/base.html' %}
 <!--add -->
{% load humanize %}
{% block content %}

...

<table class="table mt-3">
  <tr>
   ...
  </tr>
  {% for payment in payment_list %}
  <tr>
    ...
    <!-- change -->
    <td>{{ payment.price|intcomma}}</td>
    ...
  </tr>
  {% endfor %}
</table>

...

とりあえず今回はここまでにします。

次回に検索とページネーションの作成を行っていきます。


Twitter Share