ebook img

Tango with Django 2: A beginner’s guide to web development with Django 2 (v.2020-01a) PDF

438 Pages·2020·8.482 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Tango with Django 2: A beginner’s guide to web development with Django 2 (v.2020-01a)

Tango With Django 2 A beginner’s guide to web development with Django 2. Leif Azzopardi and David Maxwell Thisbookisforsaleathttp://leanpub.com/tangowithdjango2 Thisversionwaspublishedon2020-01-12 ThisisaLeanpubbook.LeanpubempowersauthorsandpublisherswiththeLean Publishingprocess.LeanPublishingistheactofpublishinganin-progressebook usinglightweighttoolsandmanyiterationstogetreaderfeedback,pivotuntilyou havetherightbookandbuildtractiononceyoudo. ©2018-2020LeifAzzopardiandDavidMaxwell Tweet This Book! PleasehelpLeifAzzopardiandDavidMaxwellbyspreadingthewordaboutthis bookonTwitter! Thesuggestedtweetforthisbookis: I’mreadyto@tangowithdjangotoo!Checkouthttps://www.tangowithdjango.com Thesuggestedhashtagforthisbookis#tangowithdjango. Findoutwhatotherpeoplearesayingaboutthebookbyclickingonthislinkto searchforthishashtagonTwitter: #tangowithdjango Also By These Authors Books by Leif Azzopardi HowtoTangowithDjango1.9/1.10/1.11 Books by David Maxwell HowtoTangowithDjango1.9/1.10/1.11 Contents 1. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 WhyWorkwiththisBook? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 WhatyouwillLearn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 TechnologiesandServices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Rango:InitialDesignandSpecification. . . . . . . . . . . . . . . . . . . . 5 1.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2. GettingReadytoTango . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.1 Python3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.2 VirtualEnvironments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.3 ThePythonPackageManager . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.4 IntegratedDevelopmentEnvironment . . . . . . . . . . . . . . . . . . . . 20 2.5 VersionControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.6 TestingyourImplementation . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3. DjangoBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.1 TestingYourSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.2 CreatingYourDjangoProject . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.3 CreatingaDjangoApp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.4 CreatingaView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.5 MappingURLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.6 BasicWorkflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4. TemplatesandMediaFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.1 UsingTemplates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.2 ServingStaticMediaFiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4.3 ServingMedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.4 BasicWorkflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 CONTENTS 5. ModelsandDatabases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 5.1 Rango’sRequirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 5.2 TellingDjangoaboutYourDatabase . . . . . . . . . . . . . . . . . . . . . . 66 5.3 CreatingModels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 5.4 CreatingandMigratingtheDatabase . . . . . . . . . . . . . . . . . . . . . 70 5.5 DjangoModelsandtheShell . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5.6 ConfiguringtheAdminInterface . . . . . . . . . . . . . . . . . . . . . . . . 75 5.7 CreatingaPopulationScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.8 Workflow:ModelSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 6. Models,TemplatesandViews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 6.1 Workflow:AData-DrivenPage . . . . . . . . . . . . . . . . . . . . . . . . . . 91 6.2 ShowingCategoriesonRango’sHomepage . . . . . . . . . . . . . . . . . 91 6.3 CreatingaDetailsPage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 7. Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.1 BasicWorkflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.2 PageandCategoryForms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 8. WorkingwithTemplates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 8.1 UsingRelativeURLsinTemplates . . . . . . . . . . . . . . . . . . . . . . . 131 8.2 DealingwithRepetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 8.3 TemplateInheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 8.4 Therender()MethodandtherequestContext . . . . . . . . . . . . . . . 142 8.5 CustomTemplateTags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 8.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 9. UserAuthentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.1 SettingupAuthentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.2 PasswordHashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 9.3 PasswordValidators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 9.4 TheUserModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 9.5 AdditionalUserAttributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 9.6 CreatingaUserRegistrationViewandTemplate . . . . . . . . . . . . . . . 154 9.7 ImplementingLoginFunctionality. . . . . . . . . . . . . . . . . . . . . . . 163 9.8 RestrictingAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 CONTENTS 9.9 LoggingOut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 9.10 Tidyingupthebase.htmlHyperlinks . . . . . . . . . . . . . . . . . . . . . 173 9.11 TakingitFurther . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 10. CookiesandSessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 10.1 Cookies,CookiesEverywhere! . . . . . . . . . . . . . . . . . . . . . . . . . . 176 10.2 SessionsandtheStatelessProtocol . . . . . . . . . . . . . . . . . . . . . . 179 10.3 SettingupSessionsinDjango . . . . . . . . . . . . . . . . . . . . . . . . . . 181 10.4 ACookieTastingSession . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 10.5 Client-SideCookies:ASiteCounterExample . . . . . . . . . . . . . . . . 183 10.6 SessionData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 10.7 Browser-LengthandPersistentSessions . . . . . . . . . . . . . . . . . . 190 10.8 ClearingtheSessionsDatabase . . . . . . . . . . . . . . . . . . . . . . . . . 190 10.9 BasicConsiderationsandWorkflow . . . . . . . . . . . . . . . . . . . . . . 191 11. UserAuthenticationwithDjango-Registration-Redux . . . . . . . . . . . . . . 194 11.1 SettingupDjangoRegistrationRedux. . . . . . . . . . . . . . . . . . . . . 194 11.2 FunctionalityandURLmapping . . . . . . . . . . . . . . . . . . . . . . . . 196 11.3 SettinguptheTemplates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 12. BootstrappingRango . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 12.1 TheTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 12.2 QuickStyleChange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 13. AddingSearchtoRango . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 13.1 TheBingSearchAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 13.2 AddingSearchFunctionality . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 13.3 PuttingSearchintoRango . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 14. MakingRangoTangoExercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 14.1 TrackingPageClickthroughs . . . . . . . . . . . . . . . . . . . . . . . . . . 240 14.2 SearchingWithinaCategoryPage . . . . . . . . . . . . . . . . . . . . . . . 242 14.3 CreateandViewUserProfiles . . . . . . . . . . . . . . . . . . . . . . . . . . 244 15. MakingRangoTangoHints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 15.1 TrackPageClickthroughs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 15.2 SearchingWithinaCategoryPage . . . . . . . . . . . . . . . . . . . . . . . 249 CONTENTS 15.3 CreatingaUserProfileInstance . . . . . . . . . . . . . . . . . . . . . . . . . 255 15.4 Class-BasedViews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 15.5 ViewingyourProfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 15.6 ListingallUsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 16. JQueryCrashCourse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 16.1 IncludingJQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 16.2 TestingyourSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 16.3 FurtherDOMManipulationExamples . . . . . . . . . . . . . . . . . . . . 289 16.4 DebuggingHints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 17. AJAXinDjangowithJQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 17.1 AJAXandRango . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 17.2 Addinga“Like”Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 17.3 AddingInlineCategorySuggestions . . . . . . . . . . . . . . . . . . . . . . 303 17.4 FurtherAJAX-ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 18. AutomatedTesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 18.1 RunningTests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 18.2 ExaminingTestingCoverage . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 19. DeployingYourProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 19.1 CreatingaPythonAnywhereAccount . . . . . . . . . . . . . . . . . . . . . 328 19.2 ThePythonAnywhereWebInterface . . . . . . . . . . . . . . . . . . . . . 329 19.3 CreatingaVirtualEnvironment . . . . . . . . . . . . . . . . . . . . . . . . . 330 19.4 SettingupyourWebApplication . . . . . . . . . . . . . . . . . . . . . . . . 335 19.5 LogFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 20. FinalThoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 20.1 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Appendices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 SettingupyourSystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 InstallingPython3andpip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 VirtualEnvironments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Usingpip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 CONTENTS VersionControlSystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 ACrashCourseinUNIX-basedCommands . . . . . . . . . . . . . . . . . . . . . . . 367 UsingtheTerminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 CoreCommands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 AGitCrashCourse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 WhyUseVersionControl? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 HowGitWorks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 SettingupGit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 BasicCommandsandWorkflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 RecoveringfromMistakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 ACSSCrashCourse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 IncludingStylesheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 BasicCSSSelectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 ElementSelectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 ColoursandBackgrounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Containers,Block-LevelandInlineElements . . . . . . . . . . . . . . . . . . . . 408 BasicPositioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 TheBoxModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 StylingLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 StylingLinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 TheCascade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 AdditionalReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428 1. Overview This book aims to provide you with a practical guide to web development using Django 2 and Python 3. The book is designed primarily for students, providing a walkthroughofthestepsinvolvedingettingawebapplicationupandrunningwith Django. However, anyone who’s starting off with web development will find this booktobebeneficial. ThisbookseekstocomplementtheofficialDjangoTutorials¹andmanyoftheother excellent tutorials available online. By putting everything together in one place, thisbookfillsinmanyofthegapsintheofficialDjangodocumentationbyproviding an example-based, design-driven approach to learning the Django framework. Furthermore, this book provides an introduction to many of the aspects required tomasterwebapplicationdevelopment(suchasHTML,CSSandJavaScript). 1.1 Why Work with this Book? This book will save you time. On many occasions we’ve seen clever students get stuck, spending hours trying to fight with Django and other aspects of web development. More often than not, the problem was usually because a key piece of information was not provided, or something was not made clear. While the occasionalblipmightsetyouback10-15minutes,sometimestheycantakehours to resolve. We’ve tried to remove as many of these hurdles as possible. This will meanyoucangetonwithdevelopingyourapplicationinsteadofgettingstuck. This book will lower the learning curve. Web application frameworks can save you a lot of hassle and a lot of time. But that is only true if you know how to use theminthefirstplace!Oftenthelearningcurveissteep.Thisbooktriestogetyou going – and going fast – by explaining how all the pieces fit together and how to buildyourwebapplogically. ¹https://docs.djangoproject.com/en/2.1/intro/tutorial01/

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.