This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

[Day 38]毎日続けるために方針を変えました

Last updated at Posted at 2021-02-21

February 21, 2021
←前回:Day 37 Djangoで移動できるWebサイトを作成する

「Djangoを一から学びたい」とのことでありましたら[Day 1]Djangoの開発環境から読むことをおすすめします。

はじめに

これは日記として書いています。
誰かの役に立つことを想定していません。
技術的な参考にはならないかと思うので、他のQiitaの記事を読むことをおすすめします。

今日やったこと

・_footer.htmlの追加
・base.htmlの追加
・〇〇さんようこそ!の作成
・/aboutのページのバリエーションを増やす
  ・スキルの表示
  ・名前の選別
  ・現在時刻の表示
・_footerの変更
の計5つです。

_footer.htmlの追加

templatesに新たなHTMLファイルを作成した。
これはただのHTMLです。

_footer.html
<div>
  <p>Copyright 2018-</p>
</div>

base.htmlの追加

base.htmlも追加
今後はこのbase.htmlをベースにHTMLを作成していきます。

base.html
{% load static %}
<!doctype HTML>
<html>
    <head>
      <link rel="stylesheet" href="{% static 'main.css' %}">

    </head>
    <body>
      {% include '_navber.html' %}
      {% block main %}
      {% endblock %}
      {% include '_footer.html' %}
    </body>
</html>

cssを使ったらloadが必要になったので追加しました。(1行目)
あと、blockタグを使ってindex.htmlとabout.htmlの方をシンプルにしました。

index.htmlとabout.htmlの変更
indexの方には画像を追加しておきました。

templates/index.html
{% extends 'base.html' %}
{% load static %}

{% block main %}
<h1>ホーム</h1>
<img src="{% static 'welcome.jpg' %}" />
{% endblock %}
templates/about.html
{% extends 'base.html' %}

{% block main %}
<h1>自己紹介</h1>
{% endblock %}

〇〇さんようこそ!の作成

〇〇さんようこそ!としたいのでindex.htmlを変更し、views.pyに関数を追加しました。

templates/index.html
{% block main %}
<h1>ホーム</h1>
<h1>{{ username }}さんようこそ!</h1>
<img src="{% static 'welcome.jpg' %}" />
{% endblock %}
website/views.py(一部抜粋)
class IndexView(TemplateView):
  template_name = 'index.html'

  def get_context_data(self):
    ctxt = super().get_context_data()
    ctxt['username'] = '名無し'
    return ctxt

/aboutのページのバリエーションを増やす

・カンマ区切りの数字を表示する

templates/about.html
{% extends 'base.html' %}
{% load humanize %}

{% block main %}
<h1>自己紹介</h1>
<h2>実績</h2>
<p>これまでに{{ num_services|intcomma }}個のサービスを作りました!</p>
<h2>スキル</h2>
<ul>
  <li>Python</li>
  <li>C言語</li>
  <li>Swift</li>
  <li>Java</li>
  <li>PHP</li>
</ul>
{% endblock %}

viewを変えます。
いつもと一緒です。

website/views.py(一部抜粋)
class AboutView(TemplateView):
  template_name = 'about.html'

  def get_context_data(self):
    ctxt = super().get_context_data()
    ctxt['num_services'] = 12345678
    return ctxt

settingの追加

about.htmlでhumanizeをloadしたのでsettingに追加していきます。

setting.py(一部抜粋)
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
+   'django.contrib.humanize',
    'website',
]

・スキルの表示

viewの追加

views.py(一部抜粋)
  def get_context_data(self):
    ctxt = super().get_context_data()
    ctxt['num_services'] = 12345678
    ctxt['skills'] = [
      'Python',
      'C',
      'Swift',
      'PHP',
      'Go',
    ]
    return ctxt

about.htmlの変更

about.html(一部抜粋)
<h2>スキル</h2>
<ul>
    {% for skill in skills %}
    <li>{{ skill }}</li>
    {% empty %}
    <li>スキルがありません</li>
    {% endfor %}
</ul>
{% endblock %}

・名前の選別

index.htmlの変更

ここでは名前の選別をしたというよりも、if文を学んだということになります。

index.html(一部抜粋)
{% block main %}
<!-- <h1>{{ username }}さんようこそ!</h1> -->
{% if username == 'Kizashi' %}
<h1>自分です</h1>
{% elif username %}
<h1>{{ username }}さん,ようこそ!</h1>
{% else %}
<h1>匿名さん,ようこそ!</h1>
{% endif %}

・現在時刻の設定

about.htmlの変更

about.html(一部抜粋)
<h1>自己紹介</h1>
<h2>実績</h2>
<p>{% now 'Y-m-d H時i分' %}現在までに{{ num_services|intcomma }}個のサービスを作りました!</p>
<h2>スキル</h2>

setting.pyの変更

setting.py(一部抜粋)
TIME_ZONE = 'Asia/Tokyo'

・_footerの変更

_footer.html
<div>
  <p>Copyright 2018-{% now 'Y' %}</p>
</div>

おわりに

今後は毎日続けれるように工夫したいと思います。
まずそのためには、自分のできる最低限までレベルを下げることが重要なので、今までのような解説口調はやめようと思います。(かなり大変なので。。。)
今日やったことをすべて載せて、書き方が多少汚くても構わず進めていきます。

それではまたまた

←前回:Day 37 Djangoで移動できるWebサイトを作成する
→次回:Day 39 djangoで静的ファイルの使用方法

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