ebook img

Django unleashed PDF

835 Pages·2016·25.525 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 Django unleashed

Django Unleashed This page intentionally left blank Django Unleashed Andrew Pinkham 800East96thStreet,Indianapolis,Indiana46240USA DjangoUnleashed Editor-in-Chief Copyright©2016byPearsonEducation,Inc. MarkL.Taub AcquisitionsEditor Allrightsreserved.Nopartofthisbookshallbereproduced,storedinaretrieval DebraWilliamsCauley system,ortransmittedbyanymeans,electronic,mechanical,photocopying, recording,orotherwise,withoutwrittenpermissionfromthepublisher.Nopatent DevelopmentEditor liabilityisassumedwithrespecttotheuseoftheinformationcontainedherein. ChrisZahn Althougheveryprecautionhasbeentakeninthepreparationofthisbook,the publisherandauthorassumenoresponsibilityforerrorsoromissions.Norisany ManagingEditor liabilityassumedfordamagesresultingfromtheuseoftheinformationcontained JohnFuller herein. ProjectEditor ISBN-13:978-0-321-98507-1 ElizabethRyan ISBN-10:0-321-98507-9 CopyEditor TheLibraryofCongresscataloging-in-publicationdataisavailableathttp://lccn. CarolLallier loc.gov/2015033839. PrintedintheUnitedStatesofAmerica Indexer Firstprinting,October2015 JohnS.Lewis Proofreader Trademarks LindaBegley Alltermsmentionedinthisbookthatareknowntobetrademarksorservicemarks havebeenappropriatelycapitalized.SamsPublishingcannotattesttotheaccuracyof EditorialAssistant thisinformation.Useofaterminthisbookshouldnotberegardedasaffectingthe KimBoedigheimer validityofanytrademarkorservicemark. CoverDesigner WarningandDisclaimer MarkShirar Everyefforthasbeenmadetomakethisbookascompleteandasaccurateas Compositor possible,butnowarrantyorfitnessisimplied.Theinformationprovidedisonan DiacriTech “asis”basis.Theauthorandthepublishershallhaveneitherliabilitynor responsibilitytoanypersonorentitywithrespecttoanylossordamagesarisingfrom theinformationcontainedinthisbookorprogramsaccompanyingit. SpecialSales Forinformationaboutbuyingthistitleinbulkquantities,orforspecialsales opportunities(whichmayincludeelectronicversions;customcoverdesigns;and contentparticulartoyourbusiness,traininggoals,marketingfocus,orbranding interests),pleasecontactourcorporatesalesdepartmentatcorpsales@pearsoned.com or(800)382-3419. Forgovernmentsalesinquiries,[email protected]. ForquestionsaboutsalesoutsidetheU.S.,[email protected]. Contents Preface xiii Acknowledgments xvii AbouttheAuthor xix I Django’sCoreFeatures 1 1 StartingaNewDjangoProject:BuildingaStartup CategorizerwithBlog 3 1.1 Introduction 3 1.2 WebsiteBasics 4 1.3 UnderstandingModernWebsites 5 1.4 BuildingModernWebsites:TheProblemsThat FrameworksSolveandTheirCaveats 6 1.5 Django:PythonWebFramework 8 1.6 DefiningtheProjectinPartI 11 1.7 CreatingaNewDjangoProjectandDjangoApps 13 1.8 PuttingItAllTogether 21 2 HelloWorld:BuildingaBasicWebpagein Django 23 2.1 Introduction 23 2.2 CreatingandIntegratingaNewApp 24 2.3 BuildingHelloWorld 25 2.4 DisplayingHelloWorld 26 2.5 ControllerLimitations:TheAdvantagesofModels andViews 27 2.6 RemovingOurHelloworldAppfromOurProject 27 2.7 PuttingItAllTogether 29 3 ProgrammingDjangoModelsandCreatingaSQLite Database 31 3.1 Introduction 31 3.2 WhyUseaDatabase? 32 3.3 OrganizingOurData 32 3.4 SpecifyingandOrganizingDatainDjango UsingModels 36 vi Contents 3.5 UsingDjangotoAutomaticallyCreateaSQLite Databasewithmanage.py 49 3.6 ManipulatingDataintheDatabase:Managers andQuerySets 56 3.7 StringCaseOrdering 68 3.8 PuttingItAllTogether 71 4 RapidlyProducingFlexibleHTMLwithDjango Templates 73 4.1 Introduction 73 4.2 RevisitingHelloWorld:TheAdvantagesof Templates 74 4.3 UnderstandingDjangoTemplatesand TheirGoals 77 4.4 ChoosingaFormat,anEngine,andaLocation forTemplates 77 4.5 BuildingaFirstTemplate:ASingleTag Object 78 4.6 BuildingtheRestofOurAppTemplates 90 4.7 UsingTemplateInheritanceforDesign Consistency 102 4.8 UsingTemplatesinPythonwiththeTemplate, Context,andloaderClasses 112 4.9 PuttingItAllTogether 118 5 CreatingWebpageswithControllers inDjango:ViewsandURLConfigurations 121 5.1 Introduction 121 5.2 ThePurposeofViewsandURL Configurations 122 5.3 Step-by-StepExaminationofDjango’sUseof ViewsandURLConfigurations 126 5.4 BuildingTagDetailWebpage 128 5.5 Generating404ErrorsforInvalid Queries 132 5.6 ShorteningtheDevelopmentProcesswith DjangoViewShortcuts 135 5.7 URLConfigurationInternals:AdheringtoApp Encapsulation 143 5.8 ImplementingtheViewsandURLConfigurations totheRestoftheSite 148 Contents vii 5.9 Class-BasedViews 155 5.10 RedirectingtheHomepage 163 5.11 PuttingItAllTogether 166 6 IntegratingModels,Templates,Views,andURL ConfigurationstoCreateLinksbetween Webpages 169 6.1 Introduction 169 6.2 GeneratingURLsinPythonandDjango Templates 170 6.3 UsingtheurlTemplateTagtoBuildaNavigation Menu 175 6.4 LinkingListPagestoDetailPages 177 6.5 CreatingLinksontheObjectDetailPages 184 6.6 RevisitingHomepageRedirection 186 6.7 PuttingItAllTogether 187 7 AllowingUserInputwithForms 189 7.1 Introduction 189 7.2 DjangoFormsasStateMachines 190 7.3 CreatingTagForm,aFormforTagObjects 190 7.4 BuildingtheFormsforStartup,Newslink,and PostModels 206 7.5 PuttingItAllTogether 210 8 DisplayingFormsinTemplates 211 8.1 Introduction 211 8.2 CreatingaNewTemplatetoCreate TagObjects 211 8.3 CreatingaNewTemplatetoUpdate TagObjects 224 8.4 CreatingaNewTemplatetoDelete TagObjects 226 8.5 CreatingTemplatesforStartupForm, NewsLinkForm,andPostForm 227 8.6 ReconsideringTemplateInheritance 229 8.7 PuttingItAllTogether 231 9 ControllingFormsinViews 233 9.1 Introduction 233 9.2 WebpagesforCreatingObjects 233 viii Contents 9.3 WebpagesforUpdatingObjects 256 9.4 WebpagesforDeletingObjects 268 9.5 PuttingItAllTogether 276 10 RevisitingMigrations 279 10.1 Introduction 279 10.2 LastWeek’sEpisode(ReviewingChapter3) 279 10.3 DataMigrations 280 10.4 SchemaMigrations 288 10.5 PuttingItAllTogether 296 11 BendingtheRules:TheContactUs Webpage 299 11.1 Introduction 299 11.2 CreatingacontactApp 300 11.3 CreatingtheContactWebpage 301 11.4 SplittingOrganizerurls.py 308 11.5 PuttingItAllTogether 310 12 TheBigPicture 313 12.1 Introduction 313 12.2 Django’sCore 313 12.3 WebpageswithViewsandURL Configurations 316 12.4 GeneratingWebpagesThankstoModels andTemplates 317 12.5 InteractingwithDataviaForms 318 12.6 InterveninginControlFlow 319 12.7 MovingForward 319 II DjangosContributedLibraries 321 13 Django’sContributedLibrary 323 13.1 Introduction 323 13.2 Django’sSourceCode(andVersioning) 323 13.3 Django’scontribCode 325 13.4 Content(Not)Covered 327 13.5 Translation 328 13.6 PuttingItAllTogether 329 Contents ix 14 Pagination:AToolforNavigation 331 14.1 Introduction 331 14.2 AWordaboutURLs:QueryversusPath 332 14.3 DiscoveringDjangoPaginationintheShell 333 14.4 PaginatingtheStartupListWebpage 337 14.5 PaginationofTagListWebpageUsingthe URLPath 345 14.6 PuttingItAllTogether 351 15 CreatingWebpageswithDjangoFlatpages 353 15.1 Introduction 353 15.2 EnablingFlatpages 353 15.3 AnatomyoftheApp 355 15.4 BuildinganAboutWebpage 355 15.5 LinkingtoFlatPageObjects 363 15.6 SecurityImplicationsofFlatPages 363 15.7 MigrationsforSitesandFlatpages 365 15.8 PuttingItAllTogether 371 16 ServingStaticContentwithDjango 373 16.1 Introduction 373 16.2 AddingStaticContentforApps 374 16.3 AddingStaticContentfortheProject 376 16.4 IntegratingRealCSSContent 377 16.5 PuttingItAllTogether 381 17 UnderstandingGenericClass-BasedViews 383 17.1 Introduction 383 17.2 BuildingGenericObjectDetailPages 384 17.3 WhyUseClassesforGenericViews? 393 17.4 BuildingGenericObjectCreatePages 394 17.5 ReplacingCBVswithGCBVs 395 17.6 ForgoingGCBVs 400 17.7 AddingBehaviorwithGCBV 401 17.8 PuttingItAllTogether 416 18 AdvancedGenericClass-BasedViewUsage 417 18.1 Introduction 417 18.2 RapidReviewofGCBV 418

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.