Contents
スポンサードリンク
最近djangoの勉強の一貫として公式tutirialを始めたのですが、思わぬところでつまづいたのでメモ。
めんどくさがらずに最初から丁寧にやっておけばこんなことにはならなかったと思う笑。
参考にしているドキュメント
一つ目のがdjangoの公式ドキュメントで、二つ目がその公式ドキュメントを元に作成されたtutorialなのでチュートリアルのチュートリアルみたいな感じになってます。
ついでに私のコードはgithubに置いておきます。
問題点
参考にしているドキュメント(チュートリアル(4))では以下のような画面になるといっていますが、ここまで順番通りにやってきたはずなのになぜか選択肢のラジオボタンが表示されない。
上のような感じで表示されるはずが、
自分の場合こんな感じに。(質問名が違うのは適当に自分で名付けたので気にしないでください。)
というよりその選択肢の文章どこで設定してんだよって感じで、本家チュートリアル(一つ目の参考ドキュメント)をくまなく探す。
一箇所飛ばしていることに気づく笑。
スポンサードリンク
飛ばしていた箇所
本家チュートリアル(2)の真ん中らへん「APIで遊んでみる」というところ。
APIなんかで今は遊んでらんねぇ、と当初はすっ飛ばしていましたがここがこんなに重要だとは。
pythonシェルを起動し以下をコピペしていく。
1 |
python manage.py shell |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
>>> from polls.models import Question, Choice # Make sure our __str__() addition worked. >>> Question.objects.all() <QuerySet [<Question: What's up?>]> # Django provides a rich database lookup API that's entirely driven by # keyword arguments. >>> Question.objects.filter(id=1) <QuerySet [<Question: What's up?>]> >>> Question.objects.filter(question_text__startswith='What') <QuerySet [<Question: What's up?>]> # Get the question that was published this year. >>> from django.utils import timezone >>> current_year = timezone.now().year >>> Question.objects.get(pub_date__year=current_year) <Question: What's up?> # Request an ID that doesn't exist, this will raise an exception. >>> Question.objects.get(id=2) Traceback (most recent call last): ... DoesNotExist: Question matching query does not exist. # Lookup by a primary key is the most common case, so Django provides a # shortcut for primary-key exact lookups. # The following is identical to Question.objects.get(id=1). >>> Question.objects.get(pk=1) <Question: What's up?> # Make sure our custom method worked. >>> q = Question.objects.get(pk=1) >>> q.was_published_recently() True # Give the Question a couple of Choices. The create call constructs a new # Choice object, does the INSERT statement, adds the choice to the set # of available choices and returns the new Choice object. Django creates # a set to hold the "other side" of a ForeignKey relation # (e.g. a question's choice) which can be accessed via the API. >>> q = Question.objects.get(pk=1) # Display any choices from the related object set -- none so far. >>> q.choice_set.all() <QuerySet []> # Create three choices. >>> q.choice_set.create(choice_text='Not much', votes=0) <Choice: Not much> >>> q.choice_set.create(choice_text='The sky', votes=0) <Choice: The sky> >>> c = q.choice_set.create(choice_text='Just hacking again', votes=0) # Choice objects have API access to their related Question objects. >>> c.question <Question: What's up?> # And vice versa: Question objects get access to Choice objects. >>> q.choice_set.all() <QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]> >>> q.choice_set.count() 3 # The API automatically follows relationships as far as you need. # Use double underscores to separate relationships. # This works as many levels deep as you want; there's no limit. # Find all Choices for any question whose pub_date is in this year # (reusing the 'current_year' variable we created above). >>> Choice.objects.filter(question__pub_date__year=current_year) <QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]> # Let's delete one of the choices. Use delete() for that. >>> c = q.choice_set.filter(choice_text__startswith='Just hacking') >>> c.delete() |
これでもう一度.以下を実行。
1 |
python manage.py runserver 0.0.0.0:8000 |
いけました!!
しかし選択してvoteを押すと
なに?
本来ならば投票がカウントされボタンを押した回数分数値が反映されるはずなのですが、反映されないどころかエラー。念のため、
http://192.168.33.15:8000/polls/1/results/で結果画面を確認すると
やはり0のまま。どこが違うのかわからない。当分はここのバグを見つけるで時間を費やしそう。
解決できたらまた更新します。