6
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 5 years have passed since last update.

初心者がDjangoのチュートリアルで何をやり何を考えているのか(2)-1

Last updated at Posted at 2018-12-06

目的

これからプログラミングを学ぼうとする初心者が何をやり何を考えているか記録する備忘録。プラス記事を書くことで最後までやり通すためのモチベーション維持。どこか間違えてたり、アホなことしてたりしたら、アドバイス等いただけると幸いです。

環境

  • ThinkPad T440p
  • Ubuntu Linux 18.04 日本語 Remix
  • Python 3.6.7
  • pip 9.0.1
  • Django 2.1.3

参考サイト(先人たちに感謝を)

0. いきなり横道にそれる(pipがインストールできない)

Visual Studio Code を使っていたら「いいextentionあるよ」とlintを薦められた。lint も初耳でググると「ソースコードに厳密なチェックをしてくれるプログラム」とのこと。何事も経験とextentionをインストールしようかと思ったらThere is no Pip installer available...とVSCodeさんに言われた。ならpipを仮想環境だけでなく通常の環境でもインストールしてみるかと思ったらできない。no named module pip と出てくる。なぜ?最新のPythonだったらpipは含まれてるよとかいう話だったのに。

初心者はあるはずのモノがないと途方に暮れる

仕方がないのでググるとどうやらget-pip.pyを使ってインストールするらしい。→ https://packaging.python.org/tutorials/installing-packages/

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

curl も初耳だ。「HTTPアクセスをしてコンテンツを取得できるコマンド」らしい。すごく便利なコマンドらしいがまだ使い方がよくわからないので横に置いておく。で、get-pip.pyの方法でインストールするのは気をつけなよとここに書いてあるけど
warning2.png
この画像を切り抜くだけでも四苦八苦だ。→ https://synclogue-navi.com/gimp-trimming

初心者は何をどう気をつけていいのかわからない。不安を煽らないでくれないか。

それでもsudo python get-pip.pyを実行。インストール自体は成功したようだがこんなことをbashさんに言われる。

The directory '/home/soh506/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/soh506/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with its parent directory is not owned by the current usersudo, you may want sudo's -H flag.

初心者は自分しか使ってないのにこれは君のディレクトリじゃないですよと言われても困る

該当ディレクトリまで飛んでいきchmode 775を/pip/httpの両ディレクトリに施した。もしかするとセキュリティ上何か問題が生じるのかもしれないがわからない。で、pip --versionで調べる。pip 18.01を確認。が、Python2.7の模様。のちのち困ることになるかもしれないのであらためてsudo python3 get-pip.pyを実行。やはりエラーが出たりしたもののインストールは無事成功したっぽい。ちゃんとPython3.6のパッケージになってたみたいだし。エラー放置で根本的に解決していないが、とりあえず動くようなのでそのまま進む。なぜならこれでまだDjango Tutorial(2)は始まってないのである。

初心者は横道に逸れると次々罠にハマり抜け出すことができない


ようやくDjango Tutorial (2) を開始。

1. データベースを作成する

/pollsdirectory/pollsproject/ にて/pollsproject/setting.py を編集する。LANGUAGE_CODE を ja に TIME_ZONE を Asia/Tokyo に変更。

$ python manage.py migrate

エラーもなく無事作成されたようだ。今は備え付けのSQLiteだが、いつの日かMySQLとかPostgreSQLとか使うときが来るんだろうか。そのときもすんなり動くとは限らない。

2. Modelをつくる

/pollsapp/models.pyにモデルを入力していく。

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

ここからオブジェクト指向プログラミングとかいうやつだ。本来ならここで立ち止まり、じっくり取り組み理解するべきなんだろうが...ちょっと面倒くさくなったのでうろ覚えの知識とざっと見のTutorialの斜め読みと適当さで自分なりの意訳をしておく。あとでもう一度勉強し直して修正しよう。

  • djangoのデータベースからmodelsをインポートせよ
  • クラスつくれ Questionってやつ これって models.Modelのサブクラスね
  • question_text っていうのは CharFieldのインスタンスね 文字が入力できる
  • pub_date っていうのはDateTimeFieldのインスタンス
  • クラスつくれ Choiceってやつ こいつもサブクラス
  • question っていいうのはForeignKeyのインスタンスね でQuestionとつなげて 消すときは一緒に削除ね → https://narito.ninja/detail/73/#modelscascade-
  • choice_text っていうのはCharFieldのインスタンスね 文字が入力できる
  • votes っていうのはIntegerFieldのインスタンスね 数字を入力する

3.Modelを有効にする

/pollsproject/settings.py にアプリの追加を行う。一番上が自分が作ったアプリ。自分で名前を変えておいて、それを忘れてTutorialに書かれているまま入力して、「動かない、動かない、エラーが出る」と悩んでいたのは私だ。

INSTALLED_APPS = [
    'pollsapp.apps.PollsappConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

追加後 $ python manage.py makemigrations polls で Migrations が完了。実はこのコマンドを4回繰り返した。エラーのたびにここが違う、あそこが違うと教えてくれて修正、またコマンド実行。たいていは勘違いとタイポ。そんなもん。

初心者は必ずタイポする。悪いのはプログラミングじゃない。オレの頭だ。

今日はここまで。

6
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
6
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?