[Django]チュートリアルで unsupported operand type(s) for +=: ‘NoneType’ and ‘int’と出たときに
Djangoチュートリアルをやってます。
はじめての Django アプリ作成、その 4 | Django ドキュメント | Djangoのpolls/views.pyのvote()メソッドの実装でつまずき、無理やり解決して先に進めたときの記録です。
おそらくどこかでミスしているだけなのですが、今後気付いたときに振り替えれるように残しておきます。同じところでつまずいて、先に進めたい方がいたらお役に立てれば幸いです。
現象
問題の箇所はここです。
polls/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from .models import Choice, Question
# ...
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
(はじめての Django アプリ作成、その 4 | Django ドキュメント | Djangoより引用)
Djangoのチュートリアルをやっている方は分かると思いますが、
質問に対する回答を投票するアプリを作るチュートリアルで、
上記コードは質問への回答を選択して投票する処理です。
なのですが。
これを実行すると、投票がひとつもない質問の場合に、
selected_choice.votes += 1
でExceptionが発生してしまいます。
TypeError at /polls/1/vote/
unsupported operand type(s) for +=: 'NoneType' and 'int'
メッセージ曰く、Noneにintは足せないらしい。そりゃそうです。
どうやらselected_choice.votesにはこの時点ではNoneが代入されている様子。
このselected_choiceを追ってみます。
polls/models.py
# ...
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
(はじめての Django アプリ作成、その2 | Django ドキュメント | Djangoより引用)
votesはint型、かつデフォルト値は0と定義されています。
にもかかわらず、TypeError。
ここで詰まるのももったいないので以下の二行を追記しました。
polls/views.py
# ...
else:
if selected_choice.votes == None: # 追加 : votesの変数がNoneか判定
selected_choice.votes = 0 # 追加 : 0を初期値として代入
selected_choice.votes += 1
selected_choice.save()
こうすることで、selected_choice.votesにNoneが入っているなら0を入れて初期化。
その上で、その後の+= 1で一つずつ得票数を足し上げます。
これだと無事、動きました。
もっと良い書き方や、ここでミスってるのでは?という点があればご指摘いただけると幸いです。
一通り通して戻ってきたら分かることもありそう…
本日はこのへんで。
ディスカッション
コメント一覧
まだ、コメントがありません