Upgrade to Pro — share decks privately, control downloads, hide ads and more …

第1回 Webアプリケーション基礎講座 / first-webapp-tutorial-sem...

第1回 Webアプリケーション基礎講座 / first-webapp-tutorial-seminar

あくあたん工房で実施したDjangoを用いて簡単なアプリケーションを構築するハンズオン.公式チュートリアルの内容を解説しつつ実装します( https://docs.djangoproject.com/ja/2.0/intro/ ).

Pudding

July 21, 2018
Tweet

More Decks by Pudding

Other Decks in Programming

Transcript

  1. ΞδΣϯμ !5 d 8FCΞϓϦͬͯԿʁ d 8FCΞϓϦͷ࢓૊Έ d 8FCΞϓϦέʔγϣϯϑϨʔϜϫʔΫ d .7$

    d ٳܜ d 1ZUIPOͷ෮श d %KBOHPνϡʔτϦΞϧ ٳܜʢద౓ʹʣ ࠲ֶ ԋश
  2. ˔ )5.- ˓ ߏ଄Λ࡞Δ ˔ $44 ˓ ݟͨ໨Λ͍͡Δ ˔ +BWBTDSJQU

    ˓ ಈ͖Λ෇͚Δ 8FCαΠτͷιʔείʔυ !8
  3. ˔ ୯ͳΔ)5.-ɾ$44Λฦ͚ͩ͢ˠ੩తαΠτ ˓ ϢʔβʹԠͨ͡ίϯςϯπͷมԽ͕΄΅ͳ͍ ˔ +BWBTDSJQUͰಈ͓͘໰͍߹ΘͤϑΥʔϜ͕͋Δ ˠʁʁʁ ˔ +BWBTDSJQU $BOWBTͰ͓ֆ͔͖Ͱ͖Δˠʁʁʁ

    ˔ αʔόʔଆͰಈతʹIUNM͕ੜ੒͞ΕΔˠʁʁʁ ˔ +BWBTDSJQU͕֎෦ͷ"1*αʔόͱ࿈ܞͯ͠දࣔΛ ม͑Δˠʁʁʁ ੩తαΠτͱ8FCΞϓϦ !11
  4. ˔ ωΠςΟϒΞϓϦέʔγϣϯ ˓ 7JTVBM4UVEJPɼ1Z$IBSNɼ$ISPNFͳͲ ˓ ͦͷϓϥοτϑΥʔϜ্Ͱ௚઀ಈ࡞͢ΔΞϓϦ ˔ 8FCΞϓϦέʔγϣϯ ˓ (NBJMɼ(JUIVCɼESBXJPͳͲ

    ˓ ϒϥ΢β্Ͱಈ࡞͢ΔΞϓϦ ˔ ϋΠϒϦουΞϓϦέʔγϣϯ ˓ 74$PEFɼ(JULSBLFOɼ4MBDLͳͲ ˓ ϒϥ΢βΛ಺ଂ͠ωΠςΟϒΞϓϦͷΑ͏ʹ
 ಈ࡞͢ΔΞϓϦ ˔ ϓϩάϨογϒ΢ΣϒΞοϓʢ18"ʣͳͲʜ ৭ʑͳΞϓϦέʔγϣϯ !13 http://www.google.com/
  5. ˔ αʔόʔଆͰ)5.-Λੜ੒͢ΔλΠϓ ˓ Ұൠతͳ8FCΞϓϦ ˓ +BWBɼ1)1ɼ3VCZɼ1ZUIPO౳Ͱॻ͔ΕΔ͜ͱ͕ଟ͍ ˔ ΫϥΠΞϯτଆͰ)5.-Λੜ੒͢ΔλΠϓ ˓ ผͰ"1*αʔό౳Λཱͯɼ+BWBTDSJQU౳Λ༻͍ͯ௨৴͠ɼ

    Ϣʔβͷϒϥ΢β্Ͱ)5.-Λੜ੒͢Δ ˓ 3FBDUKTͳͲ ˔ ϋΠϒϦουλΠϓ ˓ αʔόʔଆͰ)5.-Λੜ੒͠ɼ͔ͭΫϥΠΞϯτଆͰ΋ +BWBTDSJQUͰ੍ޚ͢Δ ˓ ্̎ͭΛ૊Έ߹ΘͤΔ ৭ΜͳλΠϓͷ8FCΞϓϦ !18
  6. ˔ ςϯϓϨʔτʹ஋ΛຒΊࠐΉํ͕ࣜҰൠత )5.-Λੜ੒͢ΔͬͯͲ͏͍͏͜ͱʁ !19 <li> <a href="/polls/{{ question.id }}/"> {{

    question.question_text }}</a> </li> <li> <a href="/polls/1/">Is this a pen?</a> </li> RVFTUJPOJE RVFTUJPORVFTUJPO@UFYU*TUIJTBQFO 
  7. ˔ 3VCZPO3BJMT ˓ 8FCΞϓϦͬͯݴͬͨΒ͜Εͬͯ͘Β͍
 ීٴ͍ͯ͠ΔɽϑϧελοΫϑϨʔϜϫʔΫɽ ˓ $PPLQBEɼ(JUIVCͳͲ ˔ -BSBWFM ˓

    1)1ɽϑϧελοΫϑϨʔϜϫʔΫɽ ˔ 4QSJOH'SBNFXPSL ˓ +BWBɽϑϧελοΫϑϨʔϜϫʔΫɽ ˔ %KBOHP ˓ 1ZUIPOɽϑϧελοΫϑϨʔϜϫʔΫɽ ˓ %SPQCPYɼ*OTUBHSBNͳͲ ୅දతͳ8FCΞϓϦϑϨʔϜϫʔΫ !26
  8. ˔ ͜Ε·Ͱ΋༷ʑͳϥΠϒϥϦ͕͋ͬͨ ˓ %#ૢ࡞͢ΔϥΠϒϥϦ ˓ )5.-ʹม਺ຒΊࠐΉϥΠϒϥϦ ˓ 8FCαʔόΛ࡞ΔϥΠϒϥϦʜFUD ˔ ಉ౳ͷػೳΛ୯Ұͷύοέʔδͱͯ͠ఏڙ͠ɼ


    جຊߏ੒Λશͯ४උ͍ͯ͠ΔϑϨʔϜϫʔΫ ˔ ͲͷϥΠϒϥϦΛ࢖͏͔ɼΑΓ΋ɼͲ͏͍ͬͨ
 ػೳΛ࣮૷͢Δ͔ɼʹूதͰ͖Δ ϑϧελοΫϑϨʔϜϫʔΫͬͯʁ !27
  9. ˔ ϝϦοτ ˓ ࣗ෼ͨͪͷϏδωεϩδοΫͷ࣮૷ʹूதͰ͖Δ ˓ ্ཱ͕ͪ͛଎͍ ˓ ͋Δఔ౓ͷηΩϡϦςΟͷ୲อʢॏཁʣ ˔ σϝϦοτ

    ˓ ͦͷϑϨʔϜϫʔΫʹϩοΫΠϯ͞Εͯ͠·͍ɼ
 ൈ͚ग़͢ͷ͕೉͍͠ɽ ˓ ֶशίετ͕ߴ͍ʢ΄Μ·͔ʁʣ ˓ ஌͕ࣝภΔ ϑϧελοΫͷϝϦοτɾσϝϦοτ !28
  10. ˔ 63-σΟεύονϟʢϧʔλʣ ˓ 63-ͷύεͱͦΕʹରԠ͢Δػೳͷඥ෇͚Λߦ͏ ˔ ςϯϓϨʔτΤϯδϯ ˓ )5.-ʹม਺ΛຒΊࠐΉػೳ ˔ ΦϒδΣΫτϦϨʔγϣφϧϚούʔʢ03.ʣ

    ˓ 42-Λ௚઀৮Δ͜ͱແ͘ɼ%#্ͷϨίʔυΛϓϩά ϥϛϯάతʹѻ͏͜ͱ͕ग़དྷΔػೳ ˓ 42-ΠϯδΣΫγϣϯΛ๷͍Ͱ͘ΕͨΓ΋͢Δ ؚΜͰ͍Δओͳػೳ !29
  11. ˔ ݴޠ͸޷Έ͕ͩɼকདྷੑ͸ߟྀ͓ͯ͘͠΂͖ ˓ ྫ͑͹ࠓ͔Β1FSMΛ΍Δͷ͸ͪΐͬͱʜʜ ˔ جຊతʹ͸σϑΝΫτελϯμʔυʹͳ͍ͬͯΔ ΋ͷΛબ୒͢Δ ˓ બ͹ΕΔʹ͸ඞͣཧ༝͕͋Δ ˓

    ࢖͍΍͍͢ɾߴػೳͳͲͳͲ ˔ ҉໧ͷػೳ͕ଟ͍ͷͰɼυΩϡϝϯτ͕͔ͬ͠Γ ͋Γωοτʹ৘ใ͕ଟ͍΋ͷΛબ୒͠Α͏ ϑϨʔϜϫʔΫͷબఆ !30
  12. ˔ .PEFM7JFX$POUSPMMFSʹ෼͚࣮ͯ૷͢Δ͜ͱͰɼ ͦΕͧΕͷ݁߹ΛऑΊอकੑΛߴΊΔ ˔ .PEFM ˓ σʔλͷอ࣋ɾݕূ ˔ $POUSPMMFS ˓

    σʔλͷૢ࡞ʢӕ͸͍ͭͯͳ͍ʣ ˔ 7JFX ˓ Ϣʔβʹݟ͑ΔܗͰσʔλΛදࣔʢӕ͸SZʣ .7$ !32
  13. ˔ %KBOHP͕࠾༻͍ͯ͠ΔΞʔΩςΫνϟ ˓ .PEFM5FNQMBUF7JFXʹ෼͚ͯߟ͑Δ ˔ .PEFM ˓ σʔλϕʔεʹଘࡏ͢Δσʔλ ˔ 7JFX

    ˓ ͦͷσʔλΛॲཧɾબ୒͢ΔͳͲ ˔ 5FNQMBUF ˓ σʔλΛຒΊࠐΉςϯϓϨʔτσβΠϯ .57 !36
  14. Django at a glance Because Django was developed in a

    fast-paced newsroom environment, it was designed to make common Web- development tasks fast and easy. Here’s an informal overview of how to write a database-driven Web app with Django. The goal of this document is to give you enough technical specifics to understand how Django works, but this isn’t intended to be a tutorial or reference – but we’ve got both! When you’re ready to start a project, you can start with the tutorial or dive right into more detailed documentation. ˔ ্هӳจͷ֤୯ޠͷग़ݱ਺ΛΧ΢ϯτ͢Δ ˓ DPMMFDUJPOT$PVOUFS࢖͏ͷ͸ແ͠ ୯ޠͷΧ΢ϯτ !39 Cited from https://docs.djangoproject.com/en/2.0/intro/overview/
  15. ˔ 1ZUIPOͰॻ͚Δ ˓ ػցֶशͱͷ਌࿨ੑͷ͋ΔݴޠͳͷͰྲྀߦʹ
 ৐͔ͬͬͯΔ ˔ ϑϧελοΫͰ͋Δ ˓ ߴ଎Ͱൺֱత҆શͳ։ൃͷͨΊʹ ˔

    ؅ཧαΠτͷॆ࣮౓ ˓ σϑΥϧτͰBENJO༻ͷαΠτ͕෇ଐ͍ͯ͠Δ ˓ ςετσʔλΛ࡞੒ߋ৽࡟আ͢Δͷָ͕ ˔ ೔ຊޠͰݕࡧͨ͠ͱ͖ͷ৘ใͷ·ͱ΋͞ ˓ 3˓CZ˓O3˓JMTͷݕࡧ݁Ռ͕ͻͲ͗͢Δ 8IZ%KBOHPʁ !41
  16. ˔ %KBOHP͸YͱYͰͦΕͳΓʹมΘͬͨ ˔ ࠓޙ࢖͍ͬͯ͘ͳΒ%KBOHPYҰ୒ %KBOHPͷΠϯετʔϧ !46 $ pipenv install django

    $ pipenv run python -m django --version 2.0.7 $ pipenv graph 07/18 21:34(Wed) Django==2.0.7 - pytz [required: Any, installed: 2018.5]
  17. $ pipenv run django-admin startproject sample $ tree -L 3

    sample sample ├── manage.py └── sample ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py ˔ ৽نϓϩδΣΫτ࡞੒ͳͲΛߦ͏ ˔ EKBOHPΛΠϯετʔϧ͢Δͱ࢖͑ΔΑ͏ʹͳΔ EKBOHPBENJOίϚϯυ !47
  18. $ pipenv run python manage.py runserver # ϒϥ΢βͰ http://localhost:8000/ ʹΞΫηε

    ˔ %KBOHPͷ༷ʑͳૢ࡞Λߦ͏ͨΊͷϥούʔ
 εΫϦϓτΈ͍ͨͳ΋ͷ ˓ ಠࣗίϚϯυͷఆٛ΋Մೳ ˔ %#ͷςʔϒϧ࡞੒΍։ൃ༻αʔόͷىಈͳͲ NBOBHFQZ !48
  19. # PyCharmͷmanage.py task࣮ߦΛ࢖༻͢Δ manage.py@webapp_practice > startapp polls ˔ NBOBHFQZ͔Β࢖͑ΔίϚϯυ ˔

    %KBOHPͰ͸ʮΞϓϦέʔγϣϯʯͱ͍͏୯Ґ͕༻͍Β Ε͍ͯΔ ˓ ྫ͑͹Ϣʔβ؅ཧɾೝূͳͲҰͭͷػೳ୯Ґ ˓ ΋ͪΖΜ͋ΔΞϓϦ͔ΒผͷΞϓϦͷσʔλΛࢀর
 ͢Δ͜ͱ΋ग़དྷΔ ˔ ࠓճ͸࣭໰ͱબ୒ࢶ͔ΒϢʔβ౤ථΛืΔΞϓϦ
 QPMMTΛ࡞੒͢Δ TUBSUBQQ !70
  20. sample ├── manage.py ├── polls │ ├── __init__.py │ ├──

    admin.py │ ├── apps.py │ ├── models.py │ ├── tests.py │ └── views.py └── sample ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py ΞϓϦͷσΟϨΫτϦߏ੒ !71 ৽͘͠௥Ճ͞Εͨ
  21. )FMMPXPSME͢Δ !72 ˔ ΞΫηε͢Δͱ)FMMPXPSMEͱฦ͢ϖʔδΛ࡞Δ ˓ DPNNJUBDD ˔ WJFXTQZ ˓ .7$Ͱ͍͏ίϯτϩʔϥʹ͍ۙʢ΍΍͍͜͠ʣ

    ˓ Ϣʔβ͔ΒͷϦΫΤετΦϒδΣΫτΛॲཧͯ͠
 ϨεϙϯεΛฦ͢ ˔ VSMTQZ ˓ Ͳͷ63-ύεΛͲͷWJFXʹඥ෇͚Δ͔ΛܾΊΔ ˓ JODMVEF ͸ͦ͜ʹؚ·ΕΔVSMQBUUFSOΛશͯಡΈࠐΈ ͦͷύεҎԼʹ഑ஔ͢Δࣄ͕ग़དྷΔ
  22. class Question(models.Model): """࣭໰ͷϞσϧ""" # ࣭໰จͷΧϥϜ question_text = models.CharField(max_length=200) # ࡞੒͞Εͨ೔෇ͷΧϥϜ

    pub_date = models.DateTimeField('date published') 2VFTUJPOςʔϒϧ !76 QPMMTNPEFMTQZ QL QSJNBSZLFZ RVFTUJPO@UFYU QVC@EBUF  IPHFIPHF   GVHBGVHB  ΧϥϜ Ϩίʔυ
  23. class Choice(models.Model): """બ୒ࢶͷϞσϧ""" # ରԠ͢Δ࣭໰ͷidΛ֨ೲ͢ΔΧϥϜ question = models.ForeignKey(Question, on_delete=models.CASCADE) #

    બ୒ࢶͷจষ choice_text = models.CharField(max_length=200) # ౤ථ਺ͷΧϥϜ votes = models.IntegerField(default=0) ֎෦Ωʔ੍໿ʢ'PSFJHO,FZʣ !77 QPMMTNPEFMTQZ QL DIPJDF@UFYU WPUFT RVFTUJPO@JE  QPZP    QPF   $IPJDFςʔϒϧ ඥ෇͚Δ2VFTUJPOςʔϒϧ্ ͷϨίʔυͱରԠ෇͚ %KBOHPͷ03.͔Β৮Δ্Ͱ ͸ಛʹҙࣝ͠ͳͯ͘ྑ͍ ʢΞΫηε͢Δࡍʹ͸௚઀ͦ ͷΦϒδΣΫτΛ৮ΕΔʣ
  24. ϦϨʔγϣϯͷύλʔϯ !78 ˔ .BOZUP0OF0OFUP.BOZ ˓ ྫʣຊͱग़൛ࣾɽຊ͸ଟ਺ͰͦΕͧΕͷग़൛ࣾ͸Ұͭ ˓ ࠓճ͸࣭໰ʢ0OFʣͱબ୒ࢶʢ.BOZʣͷؔ܎ ˔ 0OFUP0OF

    ˓ ྫʣϢʔβͱͦͷϓϩϑΟʔϧ ˓ ඞͣҰ͔ͭ࣋ͯ͠ͳ͍ ˔ .BOZUP.BOZ ˓ ྫʣຊͱஶऀͷؔ܎ɽڞஶऀΛߟྀ͢ΔͱɼҰͭͷຊΛ ෳ਺ਓ͕ɼҰਓ͕ෳ਺ͷຊΛॻ͘ঢ়ଶ͕ଘࡏɽ ˓ ຊͷςʔϒϧͱஶऀͷςʔϒϧҎ֎ʹɼͦΕͷඥ෇͚Λ ఆٛ͢Δςʔϒϧ͕ඞཁʢ03.͕উखʹ΍ͬͯ͘ΕΔʣ
  25. ˔ σʔλϕʔεͷ͍Ζ͍Ζͳૢ࡞Λࢦ͢ ˓ ৽نςʔϒϧΛ࡞Δ ˓ ΧϥϜΛมߋ͢ΔʜFUD ˔ NBLFNJHSBUJPOTίϚϯυ ˓ ΞϓϦ௚ԼͷNJHSBUJPOTσΟϨΫτϦʹૢ࡞಺༰͕


    ࣗಈੜ੒͞ΕΔ ˓ σʔλϕʔε͝ͱʹ࠷దͳ΋ͷΛग़ྗ ˔ NJHSBUFίϚϯυ ˓ ੜ੒͞ΕͨϚΠάϨʔγϣϯͷ಺༰Λ࣮ߦ͢Δ ˓ ϚΠάϨʔγϣϯͰςʔϒϧΛ࡞Δ·Ͱ͸σʔλϕʔε ʹϨίʔυΛ௥ՃͰ͖ͳ͍ ϚΠάϨʔγϣϯ !79
  26. ॳճͷϚΠάϨʔγϣϯΛ࣮ߦ !80 manage.py@webapp_practice > makemigrations manage.py@webapp_practice > migrate Operations to

    perform: Apply all migrations: admin, auth, contenttypes, polls, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying polls.0001_initial... OK Applying sessions.0001_initial... OK
  27. ˔ NBOBHFQZTIFMMͰ%KBOHPͷػೳΛίϚϯυ
 ϥΠϯͰࢼͤΔ ˔ ࣭໰ͱͦΕʹඥ෇͘બ୒ࢶΛ࡞ͬͯΈΔ %KBOHPͷTIFMM !81 >>> from polls.models

    import Choice, Question >>> Question.objects.all() <QuerySet []> >>> from django.utils import timezone >>> q = Question(question_text="What's new?",pub_date=timezone.now()) >>> q.save() >>> Question.objects.all() <QuerySet [<Question: Question object (1)>]> >>> c = Choice.objects.create(question=q, choice_text="hoge") >>> q.choice_set.all() <QuerySet [<Choice: Choice object (1)>]>
  28. from django.contrib import admin from polls.models import Question, Choice #

    QuestionͱChoiceΛadminαΠτ͔Β৮ΕΔΑ͏ʹొ࿥ admin.site.register(Question) admin.site.register(Choice) QPMMTBENJOQZ ˔ σʔλϕʔεͷϨίʔυΛ؅ཧͰ͖Δศརػೳ ˓ IUUQMPDBMIPTUBENJO ˔ BENJOαΠτ΁ೖΔʹ͸·ͣ؅ཧϢʔβ͕ඞཁ ˓ DSFBUFTVQFSVTFSίϚϯυΛ࢖༻͢Δ ˔ ӾཡͰ͖ΔΑ͏ʹࣗ࡞ͷϞσϧΛొ࿥͢Δ BENJOαΠτ΁ొ࿥ !82
  29. ˔ ࣭໰Ұཡϖʔδ ˓ ࠷৽ͷ࣭໰Λ͍͔ͭ͘දࣔ ˔ ࣭໰ৄࡉϖʔδ ˓ ݁ՌΛදࣔͤͣɺ࣭໰ςΩετͱ౤ථϑΥʔϜΛදࣔ ˔ ࣭໰݁Ռϖʔδ

    ˓ ಛఆͷ࣭໰ͷ݁ՌΛදࣔ ˔ ౤ථϖʔδ ˓ ಛఆͷ࣭໰ͷબ୒Λ౤ථͱͯ͠ड෇ ࡞ΔϖʔδҰཡ !87
  30. urlpatterns = [ # Ұ෦লུ # ஫ҙɿ຤ඌͷεϥογϡΛ๨Εͳ͍Α͏ʹ # `/polls/1/` path('<int:question_id>/',

    views.detail, name='detail'), # `/polls/1/results/` path('<int:question_id>/results/', views.results, name='results'), # `/polls/1/vote/` path('<int:question_id>/vote/', views.vote, name='vote'), ] QPMMTVSMTQZ ˔ ୯७ͳ7JFXΛॻ͍ͯΞΫηεͯ͠ΈΔ ˓ DPNNJUBDCD ˔ 63-ͷهड़ͷ࢓ํʹ׳ΕΑ͏ ˓ ܕ໊ม਺໊ͰWJFXؔ਺Ͱड͚औΔ͜ͱ͕ग़དྷΔ 7JFXͷϓϩτλΠϓΛ࡞੒ !88
  31. {% if latest_question_list %} <ul> {% for question in latest_question_list

    %} {# URLΛॻ͍࣭ͯ໰΁ͷϦϯΫΛ࡞Δ #} <li><a href="/polls/{{ question.id }}/"> {{ question.question_text }}</a></li> {% endfor %} </ul> {% else %} {# ࣭໰͕Ұͭ΋ొ࿥͞Ε͍ͯͳ͔ͬͨ৔߹ #} <p>No polls are available.</p> {% endif %} ˔ %KBOHP5FNQMBUF-BOHVBHFʢ%5-ʣΛ࢖༻ ˓ \^ͱ\\^^ɼ\^Ͱғ·Εͨൣғ͸%5-ͷจ๏ ˓ +JOKBͳͲͷςϯϓϨʔτΤϯδϯ΋࢖͑Δ ˔ ม਺͸7JFX͔ΒࣙॻࣜͷΦϒδΣΫτͰ౉͞ΕΔ ˓ DPNNJUCCC )5.-ͷςϯϓϨʔτΛॻ͘ !89 QPMMTUFNQMBUFTQPMMTJOEFYIUNM औಘ࣭ͨ͠໰ͷ ݸ਺͚ͩGPSϧʔϓ͕ ࣮ߦ͞ΕΔ ˡϦετͷத਎͕ۭͳΒ'BMTFʹͳΔ
  32. ˔ ༷ʑͳγϣʔτΧοτϝιου͕ଘࡏ͢Δ ˓ SFOEFSɿϨϯμϦϯάͨ͠)5.-Λฦ͢ ˓ SFEJSFDUɿࢦఆͨ͠63-ʹϦμΠϨΫτͤ͞Δ ˓ HFU@PCKFDU@PS@ɿࢦఆ͞ΕͨϨίʔυ͕ͳ͚Ε͹ Λฦ͢ ˓

    HFU@MJTU@PS@ɿࢦఆ͞ΕͨϨίʔυʢෳ਺ʣ͕ଘ ࡏ͠ͳ͚Ε͹Λฦ͢ ˔ ͜ΕΒΛ࢖ͬͯઌ΄ͲͷίʔυΛ؆ུԽͰ͖Δ ˓ DPNNJUGEBDF EKBOHPTIPSUDVUT !91
  33. ˔ ΞΫηεͨ͠63-͕ଘࡏ͠ͳ͔ͬͨ৔߹ʹฦ͞Ε Δεςʔλείʔυ ˔ ཁૉ͕ଘࡏ͠ͳ͍৔߹ɼ͜Ε͕ฦͬͯཉ͍͠ ˓ ͱ͍͏͔ͦ͏ͳ͍ͬͯͳ͍αΠτ͸ΰϛ ˓ ͨ·ʹΛฦ͢Α͏ͳΫιαΠτ͕͋Δ ˔

    ݱࡏৄࡉϖʔδʹଘࡏ͠ͳ͍JEͰΞΫηεͯ͠΋ ͕ฦͬͯ͘Δɽ ˓ Λฦ͢Α͏ʹमਖ਼DPNNJUG ˓ ͦͷίʔυΛ؆ུԽDPNNJUDGE  !92
  34. <h1>{{ question.question_text }}</h1> <ul> {# ࣭໰ʹؚ·ΕΔશͯͷબ୒ࢶΛදࣔ͢Δ #} {% for choice

    in question.choice_set.all %} {# forͰҰͭҰͭͷબ୒ࢶʹΞΫηε #} <li>{{ choice.choice_text }}</li> {% endfor %} </ul> QPMMTUFNQMBUFTQPMMTEFUBJMIUNM ˔ ࣭໰಺༰͚ͩͰແ͘ɼͦΕʹඥ෇͘બ୒ࢶ΋දࣔ ͍ͨ͠ ˔ EFUBJMIUNMΛมߋ͢Δ ˓ DPNNJUDEE ৄࡉϖʔδʹػೳ௥Ճ !93 ϦϨʔγϣϯઌͷ ཁૉΛશͯऔಘ
  35. {# URLΛϋʔυίʔυ͢ΔͷΛࣙΊΔɽ'detail'͸urls.pyͰࢦఆͨ͠nameʹ͋ͨΔɽ #} {# ͦͷޙʹURLύε্ͷม਺Λ༩͑Δɽ͜ͷ৔߹͸࣭໰ͷidɽ #} {# ྫ͑͹URLΛมߋͨ͘͠ͳͬͯ΋urls.pyͷΈΛมߋ͢Ε͹ɼࣗಈతʹͪ͜ΒͷURL΋मਖ਼͞ΕΔɽ #} <li><a

    href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li> QPMMTUFNQMBUFTQPMMTJOEFYIUNM ˔ ݱࡏJOEFYIUNMͰ͸QPMMT\\RVFTUJPO@JE^^ ˔ ྫ͑͹QPMMTRVFTUJPO\\RVFTUJPO@JE^^ͳͲʹ มߋͨ͘͠ͳͬͨΒʁ ˓ VSMTQZ͚ͩͰͳ͘ςϯϓϨʔτʹ΋มߋ͕ඞཁ ˔ ςϯϓϨʔτ্Ͱಈతʹ63-ੜ੒͢Δػೳ͕͋Δ ˓ DPNNJUDG 63-ͷϋʔυίʔυΛࣙΊΔ !94
  36. {# 'app_name:name'ͱ͍͏ܗࣜͰࢦఆ͢Δɽ'detail'͸urls.pyͰࢦఆͨ͠nameʹ͋ͨΔɽ #} <li><a href="{% url 'polls:detail' question.id %}"> {{

    question.question_text }}</a></li> QPMMTUFNQMBUFTQPMMTJOEFYIUNM ˔ ৄࡉදࣔͷ63-ʹEFUBJMͱ͍͏໊લΛ෇͚ͨ ˔ ଞͷΞϓϦͰ΋ಉ໊͡લΛ࢖͍ͨ͘ͳͬͨΒʁʁ ˓ ͜ͷ63-ͷ໊લ෇͚Λ͜ͷΞϓϦ಺Ͱด͍ͨ͡ ˔ VSMTQZʹBQQ@OBNFͱ͍͏ม਺Λ௥Ճ͢Δ ˓ DPNNJUBCDF 63-ͷ໊લۭؒΛ෼཭͢Δ !95 QPMMTVSMTQZ app_name = 'polls' urlpatterns = [ # লུ
  37. ˔ ࣍͸Ϣʔβ͔ΒσʔλΛ1045ͯ͠΋Β͏ ˓ )5.-ͷGPSNλάΛ࢖༻͢Δ ˓ DPNNJUEFG ౤ථػೳΛ෇͚Δ !97 <h1>{{ question.question_text

    }}</h1> {# Τϥʔ͕͋Δ৔߹දࣔ #} {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} {# ౤ථϖʔδʹରͯ͠POST͢ΔϑΥʔϜ #} <form action="{% url 'polls:vote' question.id %}" method="post"> {% for choice in question.choice_set.all %} {# ಛघͳม਺forloop͸ͦͷϧʔϓதͷΧ΢ϯτΛऔಘ͢Δɽ #} <input type="radio" name="choice" id="choice{{ forloop.counter }} value="{{ choice.id }}" /> <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label> <br /> {% endfor %} <input type="submit" value="Vote" /> </form> QPMMTUFNQMBUFTQPMMTEFUBJMIUNM
  38. ˔ -JTU7JFX ˓ ˓˓PCKFDUTBMM Ͱෳ਺ͷཁૉΛऔ͖ͬͯͯදࣔ ˓ HFU@RVFSZTFU ΛΦʔόʔϥΠυ͢Δ͜ͱͰpMUFSΛט· ͤΔ͜ͱ͕Ͱ͖Δɽ ˔

    %FUBJM7JFX ˓ ˓˓PCKFDUTHFU ͰҰͭͷཁૉΛऔ͖ͬͯͯදࣔ ˔ $SFBUF7JFXɾ6QEBUF7JFXɾ%FMFUF7JFX ˓ Ϩίʔυͷ࡞੒ɾߋ৽ɾ࡟আΛߦ͏ͨΊͷ7JFX ˔ 5FNQMBUF7JFX ˓ ࠷΋൚༻తͳ7JFXɽॲཧ͸ࣗ෼Ͱॻ͘ɽ ୅දతͳ൚༻7JFX !101
  39. ˔ %KBOHPʹ͸VOJUUFTU5FTU$BTFΛܧঝͨ͠ EKBOHPUFTU5FTU$BTF͕͋Δ ˓ ࢖͍ํ͸΄΅Ұॹɽ ˔ NBOBHFQZUFTUΞϓϦ໊ͰVOJUUFTU5FTU$BTFΛ ܧঝͨ͠ςετΛࣗಈ࣮ߦ͢Δ ˔ %KBOHP͸ࣗಈͰςετ༻ͷσʔλϕʔεΛςετ

    ࣌ʹ࡞੒ɼऴྃ࣌ʹഁغͯ͘͠ΕΔ ˓ %#࡞੒ͷݖݶ͕ඞཁͳ͜ͱʹ஫ҙʢ42-JUFҎ֎ʣ ˔ ࠓճ͸࣌ؒͷ౎߹্ল͖·͢ ςετ͸ΈΜͳ΍ͬͨํ͕͍͍Α !103
  40. {# ੩తϑΝΠϧΛಡΉ͜Ή #} {% load static %} {# style.cssΛಡΈࠐΉ #}

    <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" /> QPMMTUFNQMBUFTQPMMTEFUBJMIUNM ˔ ςϯϓϨʔτͰ\MPBETUBUJD^͢ΔͱಡΈ
 ࠐΊΔΑ͏ʹͳΔ ˓ \TUBUJDΞϓϦ໊ϑΝΠϧύε^ ˔ 63-Λੜ੒͢ΔͷͰɼIUNMλάͷϦϯΫཁૉ
 ͳͲʹೖΕΔ ੩తϑΝΠϧͷಡΈࠐΈ !107
  41. ˔ ֤ςϯϓϨʔτΛຖճશͯॻ͍͍ͯΔͷ͸ແବ͕ ଟ͍ ˓ IUNMͷIFBEͰ$44΍+4ΛಡΈࠐΜͰ͍Δ෦෼ ˓ ϔομɾϑολͳͲ ˔ ܧঝͱ͍͏࢓૊Έ͕͋Δ ˔

    ޙ͔ΒίϯςϯπΛࠩ͠ࠐΈ͍ͨ෦෼ʹ
 \CMPDLίϯςϯπ໊^\FOECMPDL^Λ
 ೖΕ͓ͯ͘ ˓ DPNNJUGBE ςϯϓϨʔτͷܧঝ !108
  42. {% load static %} <!DOCTYPE html> <html lang=ja> <head> <meta

    charset="UTF-8"> {# λΠτϧϒϩοΫ #} <title>{% block title %}{% endblock %}</title> {# style.cssΛಡΈࠐΉ #} <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" /> </head> <body> {# ϝΠϯίϯςϯπΛ഑ஔ͢Δ #} {% block content %}{% endblock %} </body> </html> ϕʔεͱͳΔςϯϓϨʔτ !109 QPMMTUFNQMBUFTQPMMTCBTFIUNM
  43. {# base.htmlΛܧঝ #} {% extends 'polls/base.html' %} {# λΠτϧΛมߋ #}

    {% block title %}࣭໰Ұཡ{% endblock %} {# ϝΠϯίϯςϯπ #} {% block content %} {% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="{% url 'polls:detail' question.id %}"> {{ question.question_text }}</a></li> {% endfor %} </ul> {% else %} <p>No polls are available.</p> {% endif %} {% endblock %} ϕʔεΛܧঝͯ͠࡞੒ !110 QPMMTUFNQMBUFTQPMMTJOEFYIUNM ܧঝ͢ΔςϯϓϨʔτΛ໌ࣔ ࢦఆͨ͠CMPDLΛ ΦʔόʔϥΠυ
  44. ˔ શମͰಉ͡ϕʔεςʔϚΛ࢖༻͢Δ৔߹ ˔ ԼهͷཁྖͰͰ͖Δ ˓ ϓϩδΣΫτ௚ԼʹUFNQMBUFTσΟϨΫτϦΛ࡞੒ ˓ TFUUJOHTQZʹͦͷ৔ॴΛهड़ɾϕʔεΛஔ͘ ˓ DPNNJUCF

    TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(BASE_DIR, 'templates') ], # ҎԼུ TBNQMFTFUUJOHTQZ ϕʔεΛϓϩδΣΫτશମͰڞ༗ !111 XFCBQQ@QSBDUJDFTBNQMFTUFNQMBUFT
  45. class QuestionAdmin(admin.ModelAdmin): """࣭໰ͷAdminαΠτͰͷදࣔΛΧελϚΠζ͢ΔΫϥε""" # දࣔ͢ΔϑΟʔϧυͷॱ൪Λมߋ fields = ['pub_date', 'question_text'] #

    QuestionͱChoiceΛadminαΠτ͔Β৮ΕΔΑ͏ʹొ࿥ admin.site.register(Question, QuestionAdmin) QPMMTBENJOQZ ˔ ؅ཧαΠτͷΧελϚΠζΛهड़͢Δ ˔ ·ͣ͸؆୯ʹදࣔॱংΛม͑Δ͚ͩ ˓ DPNNJU BENJOQZ !113
  46. class QuestionAdmin(admin.ModelAdmin): """࣭໰ͷAdminαΠτͰͷදࣔΛΧελϚΠζ͢ΔΫϥε""" # දࣔ͢ΔϑΟʔϧυΛηΫγϣϯ͝ͱʹ෼͚Δ fieldsets = [ # ηΫγϣϯ໊ແ͠

    (None, {'fields': ['question_text']}), # ηΫγϣϯ໊༗Γ ('Date information', {'fields': ['pub_date']}) ] QPMMTBENJOQZ ˔ ϞσϧͷΧϥϜ͕ଟ͘ͳͬͯ͘Δͱ·ͱ·Γ͝ͱ ʹ෼͚ͨ͘ͳΔ ˔ ηΫγϣϯ໊Λ෇͚ͯ෼͚Δ͜ͱ͕ग़དྷΔ ˓ DPNNJUGB ද߲ࣔ໨ͷηΫγϣϯ෼͚ !114
  47. class ChoiceInline(admin.StackedInline): """Questionଆ͔Β΋ChoiceΛ৮ΕΔΑ͏ʹ͢Δ""" model = Choice # දࣔ਺ͱͯ͠࠷௿3͜ͿΜҎ্༻ҙ͢Δ extra =

    3 class QuestionAdmin(admin.ModelAdmin): # লུ inlines = [ChoiceInline] QPMMTBENJOQZ ˔ ࠓ͸$IPJDFଆ͔Β͔͠ϦϨʔγϣϯΛઃఆͰ͖ͳ͍ ˔ 2VFTUJPOଆ͔Β΋ࣗ༝ʹ$IPJDFΛ࡞੒͍ͨ͠ ˓ BENJO4UBDLFE*OMJOFΛ࢖༻͢Δ ˓ DPNNJUCEEE ϦϨʔγϣϯઌ͔Βͷࢀর !116
  48. class ChoiceInline(admin.TabularInline): """Questionଆ͔Β΋ChoiceΛ৮ΕΔΑ͏ʹ͢Δ""" model = Choice # දࣔ਺ͱͯ͠࠷௿3͜ͿΜҎ্༻ҙ͢Δ extra =

    3 class QuestionAdmin(admin.ModelAdmin): # লུ inlines = [ChoiceInline] QPMMTBENJOQZ ˔ දࣔ಺༰͕গͳ͍৔߹ɼΧϥϜ਺ͿΜཱͯʹੵ· ΕΔͱը໘Λѹഭ͢Δ ˔ 5BCVMBS*OMJOFΛ࢖༻͢Δ ˓ DPNNJUD ςʔϒϧͰදࣔ !118
  49. class QuestionAdmin(admin.ModelAdmin): """࣭໰ͷAdminαΠτͰͷදࣔΛΧελϚΠζ͢ΔΫϥε""" # adminͷҰཡϖʔδͰදࣔ͢Δ߲໨ list_display = ('question_text', 'pub_date', 'was_published_recently')

    # লུ QPMMTBENJOQZ ˔ ࠓ͸࣭໰಺༰͚͕ͩදࣔ͞Ε͍ͯΔ ˔ ౤ߘ೔࣌ͱ͔΋ද͍ࣔͨ͠ ˔ MJTU@EJTQMBZΛมߋ͢Δ ˓ DPNNJU ҰཡͷදࣔΛมߋ !120
  50. class QuestionAdmin(admin.ModelAdmin): """࣭໰ͷAdminαΠτͰͷදࣔΛΧελϚΠζ͢ΔΫϥε""" # লུ # ද߲ࣔ໨Λઃఆ inlines = [ChoiceInline]

    # ϑΟϧλϦϯά͢Δ߲໨ list_filter = ['pub_date'] QPMMTBENJOQZ ˔ ౤ߘ೔࣌ͰߜΓࠐΉػೳΛ෇͚Δ ˔ MJTU@pMUFSΛ࢖͏ ˓ ΧϥϜͷϦετΛ౉͢ ˓ DPNNJUCD ߜΓࠐΈػೳ !122
  51. ˔ %KBOHP(JSMT5VUPSJBM ˓ ೔ຊޠ൛͕͋Δͷ͕ಛ௃ ˓ IUUQTEKBOHPHJSMTKBQBOHJUCPPLTJPXPSLTIPQ@UVUPSJBMKQDPOUFOU IPX@UIF@JOUFSOFU@XPSLTJOEFYIUNM ˔ %KBOHPೖ໳ʢ(FFL-BC/"("/0ʣ ˓

    %KBOHP #PPUTUSBQͷνϡʔτϦΞϧ ˓ IUUQTRJJUBDPNLBLJ@LJUFNTDBEBDEDD ˔ %KBOHP#SPUIFST ˓ ΘΓͱৄ͘͠ॻ͔Ε͍ͯΔ ˓ IUUQXXXEKBOHPCSPUIFSTDPNUVUPSJBMTCMPH@BQQ ଞͷνϡʔτϦΞϧͷ঺հ !125