logo资料库

Django For Beginners 2.0.pdf

第1页 / 共308页
第2页 / 共308页
第3页 / 共308页
第4页 / 共308页
第5页 / 共308页
第6页 / 共308页
第7页 / 共308页
第8页 / 共308页
资料共308页,剩余部分请下载后查看
Django for Beginners Learn web development with Django 2.0 William S. Vincent © 2018 William S. Vincent
Contents Introduction Why Django Why this book Book Structure Book layout Conclusion Chapter 1: Initial Setup The Command Line Install Python 3 on Mac OS X Install Python 3 on Windows Install Python 3 on Linux Virtual Environments Install Django Install Git Text Editors Conclusion Chapter 2: Hello World app Initial Setup Create an app Views and URLConfs Hello, world! Git Bitbucket Conclusion Chapter 3: Pages app Initial Setup Templates Class-Based Views URLs Add an About Page 1 1 3 3 5 7 8 8 11 13 14 15 16 20 21 21 22 22 25 27 30 31 33 37 39 39 41 44 45 47
CONTENTS Extending Templates Tests Git and Bitbucket Local vs Production Heroku Additional Files Deploy Conclusion Chapter 4: Message Board app Initial Setup Create a database model Activating models Django Admin Views/Templates/URLs Adding new posts Tests Bitbucket Heroku configuration Heroku deployment Conclusion Chapter 5: Blog app Initial Setup Database Models Admin URLs Views Templates Static files Individual blog pages Tests Git Conclusion Chapter 6: Forms Forms Update Form Delete View Tests Conclusion Chapter 7: User Accounts 49 52 54 56 57 58 61 64 65 65 68 69 70 77 81 85 90 91 93 95 96 96 98 100 105 107 108 111 116 122 125 125 127 127 138 145 151 155 156
CONTENTS Login Updated homepage Logout link Signup Bitbucket Heroku config Heroku deployment Conclusion Chapter 8: Custom User Model Setup Custom User Model Forms Superuser Conclusion Chapter 9: User Authentication Templates URLs Admin Conclusion Chapter 10: Bootstrap Pages app Tests Bootstrap Signup Form Next Steps Chapter 11: Password Change and Reset Password Change Customizing password change Password reset Custom Templates Conclusion Chapter 12: Email SendGrid Custom emails Conclusion Chapter 13: Newspaper app Articles app 156 159 161 164 170 171 173 177 178 178 180 182 185 187 188 188 192 197 203 204 204 208 211 219 224 226 226 228 231 235 240 241 241 246 250 251 251
CONTENTS URLs and Views Edit/Delete Create page Conclusion Chapter 14: Permissions and Authorization Improved CreateView Authorizations Mixins Updating views Conclusion Chapter 15: Comments Model Admin Template Conclusion Conclusion Django Resources Python Books Blogs to Follow Feedback 257 262 268 276 277 277 279 281 283 285 286 286 288 295 299 301 302 302 303 303
Introduction Welcome to Django for Beginners, a project-based approach to learning web devel- opment with the Django web framework. In this book you will build five progressively more complex web applications, starting with a simple “Hello, World” app, progressing to a blog app with forms and user accounts, and finally a newspaper app using a custom user model, email integration, foreign keys, authorization, permissions, and more. By the end of this book you should feel confident creating your own Django projects from scratch using current best practices. Django is a free, open source web framework written in the Python programming language and used by millions of programmers every year. Its popularity is due to its friendliness to both beginners and advanced programmers: Django is robust enough to be used by the largest websites in the world–Instagram, Pinterest, Bitbucket, Disqus–but also flexible enough to be a good choice for early-stage startups and prototyping personal projects. This book is regularly updated and features the latest versions of both Django (2.0) and Python (3.6x). It also uses Pipenv which is now the officially recommended package manager by Python.org for managing Python packages and virtual environments. Throughout we’ll be using modern best practices from the Django, Python, and web development communities, especially the thorough use of testing. Why Django A web framework is a collection of modular tools that abstracts away much of the difficulty–and repetition–inherent in web development. For example, most websites
Introduction 2 need the same basic functionality: the ability to connect to a database, set URL routes, display content on a page, handle security properly, and so on. Rather than recreate all of this from scratch, programmers over the years have created web frameworks in all the major programming languages: Django and Flask in Python, Rails in Ruby, and Express in JavaScript among many, many others. Django inherited Python’s “batteries-included” approach and includes out-of-the box support for common tasks in web development: • user authentication • templates, routes, and views • admin interface • robust security • support for multiple database backends • and much much more This approach makes our job as web developers much, much easier. We can focus on what makes our web application unique rather than reinventing the wheel when it comes to standard web application functionality. In contrast, several popular frameworks–most notably Flask in Python and Express in JavaScript–adopt a “microframework” approach. They provide only the bare minimum required for a simple web page and leave it up to the developer to install and configure third-party packages to replicate basic website functionality. This approach provides more flexibility to the developer but also yields more opportunities for mistakes. As of 2018 Django has been under active development for over 13 years which makes it a grizzled veteran in software years. Millions of programmers have already used Django to build their websites. And this is undeniably a good thing. Web development is hard. It doesn’t make sense to repeat the same code–and mistakes–when a large community of brilliant developers has already solved these problems for us.
Introduction 3 At the same time, Django remains under active development and has a yearly release schedule. The Django community is constantly adding new features and security improvements. If you’re building a website from scratch Django is a fantastic choice. Why this book I wrote this book because while Django is extremely well documented there is a severe lack of beginner-friendly tutorials available. When I first learned Django years ago I struggled to even complete the official polls tutorial. Why was this so hard I remember thinking? With more experience I recognize now that the writers of the Django docs faced a difficult choice: they could emphasize Django’s ease-of-use or its depth, but not both. They choose the latter and as a professional developer I appreciate the choice, but as a beginner I found it so…frustrating! My goal is that this book fills in the gaps and showcases how beginner-friendly Django really is. You don’t need previous Python or web development experience to complete this book. It is intentionally written so that even a total beginner can follow along and feel the magic of writing their own web applications from scratch. However if you are serious about a career in web development, you will eventually need to invest the time to learn Python, HTML, and CSS properly. A list of recommended resources for further study is included in the Conclusion. Book Structure We start by properly covering how to configure a local development environment in Chapter 1. We’re using bleeding edge tools in this book: the most recent version of
分享到:
收藏