Repository: feldroy/two-scoops-of-django-3.x Branch: master Commit: 2579fe5d7809 Files: 292 Total size: 532.9 KB Directory structure: gitextract_y76tine9/ ├── .github/ │ └── FUNDING.yml ├── ISSUE_TEMPLATE ├── README.md ├── changelog.md └── code/ ├── chapter_00_example_01.py ├── chapter_00_example_02.py ├── chapter_01_example_01.py ├── chapter_01_example_02.py ├── chapter_01_example_03.py ├── chapter_01_example_04.py ├── chapter_01_example_05.py ├── chapter_01_example_06.py ├── chapter_01_example_07.py ├── chapter_01_example_08.py ├── chapter_02_example_01.txt ├── chapter_02_example_02.txt ├── chapter_03_example_01.txt ├── chapter_03_example_02.txt ├── chapter_03_example_03.txt ├── chapter_03_example_04.txt ├── chapter_03_example_05.txt ├── chapter_03_example_06.txt ├── chapter_03_example_07.txt ├── chapter_03_example_08.txt ├── chapter_03_example_09.txt ├── chapter_03_example_10.txt ├── chapter_04_example_01.txt ├── chapter_04_example_02.txt ├── chapter_04_example_03.py ├── chapter_04_example_04.txt ├── chapter_04_example_05.py ├── chapter_05_example_01.txt ├── chapter_05_example_02.txt ├── chapter_05_example_03.txt ├── chapter_05_example_04.py ├── chapter_05_example_05.txt ├── chapter_05_example_06.py ├── chapter_05_example_07.txt ├── chapter_05_example_08.txt ├── chapter_05_example_09.txt ├── chapter_05_example_10.txt ├── chapter_05_example_11.txt ├── chapter_05_example_12.txt ├── chapter_05_example_13.txt ├── chapter_05_example_14.py ├── chapter_05_example_15.py ├── chapter_05_example_16.py ├── chapter_05_example_17.py ├── chapter_05_example_18.py ├── chapter_05_example_19.json ├── chapter_05_example_20.py ├── chapter_05_example_21.txt ├── chapter_05_example_22.txt ├── chapter_05_example_23.txt ├── chapter_05_example_24.txt ├── chapter_05_example_25.txt ├── chapter_05_example_26.txt ├── chapter_05_example_27.txt ├── chapter_05_example_28.py ├── chapter_05_example_29.py ├── chapter_05_example_30.py ├── chapter_06_example_01.py ├── chapter_06_example_02.py ├── chapter_06_example_03.py ├── chapter_06_example_04.py ├── chapter_06_example_05.py ├── chapter_06_example_06.py ├── chapter_06_example_07.py ├── chapter_06_example_08.py ├── chapter_06_example_09.py ├── chapter_06_example_10.py ├── chapter_06_example_11.py ├── chapter_07_example_01.py ├── chapter_07_example_02.py ├── chapter_07_example_03.py ├── chapter_07_example_04.py ├── chapter_07_example_05.py ├── chapter_07_example_06.py ├── chapter_07_example_07.py ├── chapter_07_example_08.py ├── chapter_07_example_09.sql ├── chapter_07_example_10.py ├── chapter_07_example_11.py ├── chapter_08_example_01.py ├── chapter_08_example_02.py ├── chapter_08_example_03.py ├── chapter_08_example_04.py ├── chapter_08_example_05.py ├── chapter_08_example_06.html ├── chapter_08_example_07.py ├── chapter_08_example_08.html ├── chapter_08_example_09.py ├── chapter_08_example_10.py ├── chapter_08_example_11.py ├── chapter_08_example_12.py ├── chapter_08_example_13.py ├── chapter_09_example_01.py ├── chapter_09_example_02.py ├── chapter_09_example_03.py ├── chapter_09_example_04.py ├── chapter_09_example_05.py ├── chapter_09_example_06.py ├── chapter_09_example_07.py ├── chapter_10_example_01.py ├── chapter_10_example_02.py ├── chapter_10_example_03.py ├── chapter_10_example_04.py ├── chapter_10_example_05.py ├── chapter_10_example_06.html ├── chapter_10_example_07.py ├── chapter_10_example_08.py ├── chapter_10_example_09.py ├── chapter_10_example_10.html ├── chapter_10_example_11.py ├── chapter_10_example_12.html ├── chapter_10_example_13.py ├── chapter_10_example_14.py ├── chapter_11_example_01.py ├── chapter_11_example_02.py ├── chapter_11_example_03.py ├── chapter_12_example_01.py ├── chapter_12_example_02.py ├── chapter_12_example_03.py ├── chapter_12_example_04.py ├── chapter_12_example_05.py ├── chapter_12_example_06.py ├── chapter_12_example_07.py ├── chapter_12_example_08.py ├── chapter_12_example_09.py ├── chapter_12_example_10.py ├── chapter_12_example_11.py ├── chapter_12_example_12.py ├── chapter_12_example_13.py ├── chapter_12_example_14.py ├── chapter_12_example_15.py ├── chapter_12_example_16.py ├── chapter_12_example_17.html ├── chapter_12_example_18.html ├── chapter_13_example_01.py ├── chapter_13_example_02.py ├── chapter_13_example_03.html ├── chapter_13_example_04.py ├── chapter_13_example_05.py ├── chapter_13_example_06.py ├── chapter_13_example_07.py ├── chapter_13_example_08.py ├── chapter_13_example_09.py ├── chapter_13_example_10.py ├── chapter_14_example_01.txt ├── chapter_14_example_02.txt ├── chapter_14_example_03.txt ├── chapter_14_example_04.txt ├── chapter_14_example_05.py ├── chapter_14_example_06.html ├── chapter_14_example_07.py ├── chapter_14_example_08.html ├── chapter_14_example_09.py ├── chapter_14_example_10.html ├── chapter_14_example_11.html ├── chapter_14_example_12.html ├── chapter_14_example_13.html ├── chapter_14_example_14.html ├── chapter_14_example_15.html ├── chapter_14_example_16.html ├── chapter_14_example_17.html ├── chapter_14_example_18.html ├── chapter_14_example_19.html ├── chapter_14_example_20.html ├── chapter_14_example_21.html ├── chapter_14_example_22.html ├── chapter_14_example_23.html ├── chapter_14_example_24.py ├── chapter_15_example_01.html ├── chapter_15_example_02.py ├── chapter_16_example_01.html ├── chapter_16_example_02.py ├── chapter_16_example_03.html ├── chapter_16_example_04.py ├── chapter_16_example_05.html ├── chapter_16_example_06.py ├── chapter_17_example_01.py ├── chapter_17_example_02.py ├── chapter_17_example_03.py ├── chapter_17_example_04.py ├── chapter_17_example_05.py ├── chapter_17_example_06.txt ├── chapter_17_example_07.txt ├── chapter_17_example_08.txt ├── chapter_17_example_09.txt ├── chapter_17_example_10.py ├── chapter_17_example_11.py ├── chapter_17_example_12.txt ├── chapter_17_example_13.txt ├── chapter_17_example_14.txt ├── chapter_17_example_15.py ├── chapter_17_example_16.py ├── chapter_18_example_01.txt ├── chapter_18_example_02.txt ├── chapter_19_example_01.html ├── chapter_21_example_01.py ├── chapter_21_example_02.py ├── chapter_21_example_03.py ├── chapter_21_example_04.py ├── chapter_22_example_01.py ├── chapter_22_example_02.py ├── chapter_22_example_03.py ├── chapter_22_example_04.py ├── chapter_22_example_05.py ├── chapter_22_example_06.py ├── chapter_22_example_07.py ├── chapter_22_example_08.py ├── chapter_22_example_09.py ├── chapter_22_example_10.py ├── chapter_22_example_11.py ├── chapter_22_example_12.py ├── chapter_23_example_01.txt ├── chapter_23_example_02.txt ├── chapter_23_example_03.txt ├── chapter_23_example_04.txt ├── chapter_23_example_05.txt ├── chapter_23_example_06.txt ├── chapter_23_example_07.txt ├── chapter_23_example_08.txt ├── chapter_23_example_09.txt ├── chapter_23_example_10.txt ├── chapter_23_example_11.txt ├── chapter_24_example_01.txt ├── chapter_24_example_02.py ├── chapter_24_example_03.py ├── chapter_24_example_04.py ├── chapter_24_example_05.py ├── chapter_24_example_06.py ├── chapter_24_example_07.py ├── chapter_24_example_08.txt ├── chapter_24_example_09.txt ├── chapter_24_example_10.txt ├── chapter_24_example_11.py ├── chapter_25_example_01.txt ├── chapter_25_example_02.txt ├── chapter_28_example_01.py ├── chapter_28_example_02.txt ├── chapter_28_example_03.py ├── chapter_28_example_04.py ├── chapter_28_example_05.py ├── chapter_28_example_06.py ├── chapter_28_example_07.py ├── chapter_28_example_08.py ├── chapter_28_example_09.py ├── chapter_28_example_10.py ├── chapter_28_example_11.html ├── chapter_28_example_12.html ├── chapter_29_example_01.py ├── chapter_29_example_02.py ├── chapter_29_example_03.py ├── chapter_29_example_04.py ├── chapter_29_example_05.py ├── chapter_29_example_06.py ├── chapter_30_example_01.py ├── chapter_30_example_02.py ├── chapter_30_example_03.txt ├── chapter_31_example_01.txt ├── chapter_31_example_02.py ├── chapter_31_example_03.py ├── chapter_31_example_04.py ├── chapter_31_example_05.py ├── chapter_31_example_06.py ├── chapter_31_example_07.py ├── chapter_31_example_08.py ├── chapter_31_example_09.py ├── chapter_31_example_10.py ├── chapter_31_example_11.py ├── chapter_35_example_01.txt ├── chapter_35_example_02.py ├── chapter_35_example_03.py ├── chapter_35_example_04.html ├── chapter_35_example_05.py ├── chapter_35_example_06.py ├── chapter_35_example_07.py ├── chapter_35_example_08.py ├── chapter_39_example_01.txt ├── chapter_39_example_02.txt ├── chapter_39_example_03.txt ├── chapter_39_example_04.txt ├── chapter_39_example_05.txt ├── chapter_39_example_06.txt ├── chapter_39_example_07.txt ├── chapter_39_example_08.txt ├── chapter_41_example_01.py ├── chapter_41_example_02.py ├── chapter_41_example_03.py ├── chapter_41_example_04.py └── chapter_41_example_05.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/FUNDING.yml ================================================ # These are supported funding model platforms github: [pydanny] patreon: feldroy open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry liberapay: # Replace with a single Liberapay username issuehunt: # Replace with a single IssueHunt username otechie: # Replace with a single Otechie username custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] ================================================ FILE: ISSUE_TEMPLATE ================================================ ## Greetings! Hello! As a reader, you're encouraged to submit bug reports to us for errors that you find. In turn we will give you credit for your contributions in not just the e-book but also in the print paperback. This is your opportunity to have your name in one of our books as a contributor, which you are then welcome to add to your resume and LinkedIn profile. Notes: - TODOs are markers to let you know we are going to expand an area, so reporting them during the ALPHA and BETA period **doesn't** qualify as an attributable item. - `??:??` are references for areas written but still being examined by our reviewers and not yet in the published version of the book. Reporting them **doesn't** qualify as an attributable item. ## Location within the Book * Chapter or Appendix: * Section: * Subsection: Hint: Page numbers change all the time. The best way to report an issue is by chapter, section, and subsection numbers. ## Description > If grammar, enter problematic text here, otherwise delete this line. ``` If code-related, enter it within these backticks. Otherwise, delete this segment. ``` ## Possible Solutions ## Your full name so we can provide accurate credit within the book ================================================ FILE: README.md ================================================ # Two Scoops of Django 3.x The issue tracker, changelog, and code repository for Two Scoops of Django 3.x Tracking thoughts and feature requests for Two Scoops of Django 3.x in the issue tracker. Available currently in PDF formats at https://www.feldroy.com/products/two-scoops-of-django-3-x. ## Before You Submit an Issue * Be nice. :-) * Be honest. * Check the issues from the previous version (1.11) [open](https://github.com/twoscoops/two-scoops-of-django-1.11/issues?state=open) and [closed](https://github.com/twoscoops/two-scoops-of-django-1.11/issues?state=closed) to see if your issue already exists. * Check the [changelog](https://github.com/feldroy/two-scoops-of-django-3.x/blob/master/changelog.md) to see if your issue has already been resolved. * Please, please, please identify each issue by chapter, section, and subsection numbers. Since the content shifts around using page numbers to identify problems is innaccurate. * For tracking and credit purposes, **each request for correction needs to be in it's own issue. If an issue contains 2 or more issues, we may close it with a request for breaking up into more atomic issues.** **Editorial Note:** In order to make issues more readily searchable, we may alter titles and descriptions of issues as needed. ## When will this be published? It's published and you can get it at https://www.feldroy.com/products/two-scoops-of-django-3-x ================================================ FILE: changelog.md ================================================ # Two Scoops of Django Changelog This lists many, but not all the changes between TSD 1.11 and TSD 3.x. # Updates 2023-08-16 ## General - Grammar corrections - Cleanup frontmatter - Zappa now pointing at github.com/zappa/zappa ## Asynchronous Views - Finally admit that the Django 3 story for Asynchronous views was still in its infancy. ## GraphQL - Recommend Strawberry as best-of-breed Python GraphQL libraries - Mention https://github.com/mirumee/ariadne-codegen for client generation ## Acknowledgements - Give special thanks to Fábio C. Barrionuevo - Add link to Laura Gelsomino's LinkedIn profile # Updates until 2021-07-08 ## General - Grammar corrections - All form classes have `fields` defined ## Javascript - Added Next and Nuxt - Linked to Django support for HTMX and Hotwire - General cleanup ## GraphQL - Make Graphene a first class citizen - Discuss Ariadne's shortcomings ## Signals - Updated signals to Django 3.2 ## PAAS - Added render.com and divio cloud - Remove mention of Heroku ## Deployment - Added ASGI - Modernized WSGI section ## Queues - Removed django-background-tasks as it is now unmaintained - Edit, cleanup, and unleash remaining content after subsection 1.3.5 ## Continuous Integration - Updated CI to Django 3.2 # Updates until 2021-06-14 ## General - Changed our names to Daniel and Audrey Roy Greenfeld - Grammar and spelling corrections - Corrected broken links across the book ## PAAS - Add Digital Ocean App Platform - Be honest about Heroku so users see it with open eyes ## Debugging - Brought chapter in - Removed link to ancient blog post - Added mention of `breakpoint()` ## Where and how to ask for help - Added section on the official forums - Added Libera Chat IRC - Linkified a few items to make things easier on readers --- # Updates until 2021-01-27 ## General - Converted all docs links to point to Django 3.2 ## Coding Style - Changed `isort` tip box to a package tip ## Graphql - Removed trygql.com ## Security - Mention OTPs in 2FA ## Logging - Chapter has been revised and is available for readers! - Added warning box about using f-strings with loggers - Removed Python 2 mention ## Packages - Added aldjemy - Added django-stronghold - Added environment variable section with multiple packages --- # Updates until 2020-12-02 ## General Changes - Added Authorized Vendors and Distributors informations - Various spelling and grammar corrections - Removed last python 2.7-style usages of `super()` ## Database Queries - Use `breakpoint()` instead of importing pdb ## FBVs - Added Luke Plant's "Django Views the Right Way" ## User models - Corrected wrong parent class --- # Updates until 2020-07-27 ## General Changes - Grammar fixes ## Settings and Requirements - Add missing `ql` to ORM engine ## Models and Queries - JSONField is now for all database types ## Templates - Removed GitHub's 404 page from the list of being self-contained HTML, CSS, and JS ## Third-party packages - Fix code highlighting ## Security - Fix borked link ## Appendix: Resources - Added testdriven.io --- # Updates until 2020-07-09 ## General Changes - Moved first reference to indexes from chapter 6 to chapter 7 - The inevitable grammar corrections ## Settings and Requirements - Added `.zshrc` for configuring environment variables ## Best Practices for FBVs - Made use of `functools.wraps` consistent ## Best Practices for CBVs - Mixins should not inherit from `object`, that is a holdover from Python 2.7 ## Finding and Reducing bottlenecks - Reviewed content in full and added to the book ## Third-Party Packages - Add instruction for git tagging project releases ## Async Task Queues - Started on reviewing content ## Internationalization - All pages updated for Django 3.x and Python 3.8 --- # Updates until 2020-07-07 This update adds 15 new pages to Two Scoops of Django, bringing us up to 461! ## General Changes - Yet more grammar fixes ## Javascript - Added quote by DHH about the state of JavaScript in 2020 ## Admin chapter - Removed mention of long deprecated `allow_tags` property descriptor - Add warning box to use `format_html` function for enhancing security ## User model chapter - Added multiple user models - Demonstration of proxy models ## Package Used in this Book Appendix - Out of "being updated mode" - Reviewed all packages - Added new packages - Removal of deprecated packages # Updates until 2020-06-16 ## General Changes - Revised and legible bad code examples! - More grammar fixes - All use of `reverse()` function now include URL namespaces ## Coding Standards - Change focus of `Use Explicit Relative Imports` to `Understand Explicit Relative Imports` and removed `Implicit Relative Imports` ## Chapter - Settings and Requirements - Removed tipbox for `Using django-admin Instead of manage.py`. This advice has been removed from the Django docs ## Chapter - Database Model Best Practices - Removed link to blog post that used to describe how to use `DateTimeField` with logical deletes ## Chapter - Building APIs with Django Rest Framework - Added references to OpenAPI and tools - Mentioned using vanilla Django for creating APIs - Converted package tip for django-tastypie and django-jsonview into a new section for DRF alternatives - Added trailing 'r' on `rest_framework.permissions.IsAdminUser` ## Chapter - JavaScript - Changed section "Popular JavaScript Frameworks" to "Popular JavaScript Approaches" - Added description list for serverside rendering of templates - Included alpine.js and turbolinks - Added section on JSON encoding for data rendered in the template but consumed by JavaScript ## Chapter - Security - Corrected typo so PHI is now the acronym for `Protected Health Information` ## Acknowledgements - Added Bio for Haris Ibrahim --- # Updates until 2020-06-05 - Included forward by [Will Vincent](https://learndjango.com) - Began expanding the async views chapter - Made pipenv active again now that they had a formal release - Added first half of Appendix A: Chapters mentioned in this book --- # Updates until 2020-05-29 ## General - Model mommy is now model bakery ## Chapter - Models - Table layout corrections in chapter 6 - `BooleanField(null=True)` recommended instead of `NullBooleanField` ## Chapter - FBVs - Corrected return type for `check_sprinkles` function ## Chapter - Django REST Framework - Link to sources on how to rate limit - Removed material on coreapi because that project has been replaced by openapi ## Chapter - Documentation - Added myst for rendering markdown in Sphinx ## Chapter - User Model - Mention that django-authtools doesn't yet work for Django 3 ## Chapter - Security - Ponycheckup is no more. Many thanks to Sasha Romijn for providing such an invaluable - Added PyCharm Django Security --- # Updates until 2020-05-19 ## General - More grammar! - Consistent case for `URLConfs` ## Chapter - Environment Setup - Replaced "typing" with "type casting", the latter is more explicit ## Chapter - Project Setup - Added link to Cookiecutter docs ## Chapter - Databases - Took out the long removed `use_for_related_fields` attribute from manager code example ## Chapter - Templates - Fix LaTeX escape failure ## Chapter - Security - Removed Pony Checkup ## Chapter - PaaS - Added Zappa as an option - Took away statement that Elastic Beanstalk was on mod_wsgi and hence no channels ## Chapter - CI - Added MacOS to Azure Pipelines in feature table ## Chapter - Debugging - Updated IDE preferences --- # Updates until 2020-05-13 ## General changes - General grammar and spelling improvements - Updated Django version to 3.x - Updated Python version to Python 3.8/3.9 - Removed Appendix giving advice on upgrading from Python 2 to 3 - Remove mention of Mercurial. - Changed `README.rst` to `README.md` - `OS X` is now `Mac` - References to `django-admin.py` replace with `django-admin` - Changed pypi.python.org to pypi.org - Removed last vestige of `Twitter Bootstrap` in favor of `Bootstrap` - Due to problems with a number of PDF renderers on the Mac URL escaping '#' incorrectly, URLs with '#" are forwarded through our `feld.to` link shortener. The reader sees the correct URL, but clicking links takes them through feld.to. - "Don't Do This!!" code examples have new style - Use "README file" instead of README.md or README.rst - Added footer to instruct users where to submit issues - Added warnings at the front of chapters which won't be published ## Book crafting process - LaTeX datestamping now automated. Why didn't we do this years ago? - Added placeins LaTex package to the `requirements-latex.yml` file. - Updated the book build instructions. ## Note from the authors - Added Sentry - Remove Pinterest from company list ## Introduction - Removed apocryphal statement by Leonardo Da Vinci - ## Chapter - Coding Style - Added quote from "Structure and Interpretation of Computer Programs" - Removed duplicate URL - Added black - Added wemake-python-styleguide - Added isort for ordering imports - Change JS coding style standard URL to github.com/standard/standard ## Chapter - Environment Setup - Added mention of poetry, conda, and pipenv - Git is now the only tool for version control - Added Docker to summary - Warn that pip doesn't necessarily come with Python installations on Linux distros - Removed mention of `easy_install` ## Chapter - Settings and Requirements - Use of `json.load` instead of `json.loads` ## Chapter - Django Project Layout - Added "Don't Upload Environment Directories To Open Source" warning box - Now calls runserver with the correct settings argument - Removed wemake-python-styleguide as a recommendation for Django projects ## Chapter - Fundamentals of App Design - Removed extra `models.py` from app layout example - Added Tom Christie's article for refuting the service layer approach to organizing business logic ## Chapter - Database/Model Best Practices - Rewrote the enumeration types segment as Django now includes them as part of core - Mention `BooleanField` has default of `blank=True` - Added Haki Benita's excellent article on bullet proofing Django models - `null=True` for `ManyToManyField` is now recommended against as it raises a check warning - Custom default Model Managers are no longer forbidden, just an advanced technique to be wary of - Removed mention of South as a competing migrations system ## Chapter - More Forms - Added links to more useful references, removed some outdated ones - Switch from JWT library to dj-rest-auth for authentication - Remove link to csrf docs ## Chapter - Function- and Class-Based Views - Removed historical note from Django 1.5 era about not removing function-based views - Now using `path()` url route declarations instead of the old `url()` function ## Chapter - Function-Based Views - Added type annotations to views ## Chapter - Class-Based Views - Added type annotations to views - Flavor model now uses enumeration choices - Advice on Mixins inheritance clarified for Python 3 - Added Serafeim Papastefanos' excellent CBV post to the resources section - Added links to more useful references, removed some outdated ones ## Chapter - Asynchronous Views - Added stub chapter ## Chapter - Templates - Made Python code example more concise - HTML/DTL examples now use 2-space examples - Removed Python 2 era mention that `super()` accepts arguments ## Chapter - Template Tags - Marked django-wysiwyg as deprecated ## Chapter - Jinja2 - Removed statement about maturity of Jinja2 in Django, it's mature now - Switched to 2 character tabs ## Chapter - Django REST Framework - Make more explicit that routers go into `urls.py` modules - Mention Randall Degges advice about Usage-Based Plans - Mention DRF support for different versioning schemas and link to docs - Fixed link to Classy DRF site ## Chapter - Building GraphQL APIs with Graphene - New chapter! ## Chapter - JavaScript and Django - Changed the name from **Consuming REST APIs** to **JavaScript and Django** - Restructured to be about JavaScript in the Django domain, with REST APIs being a topic - Added Vanilla JS as progressive option - Moved JQuery to legacy. While, JQuery isn't legacy yet it is on the way to become so - Rewrote strengthening JavaScript skills - Removed section on `CSRF_COOKIE_HTTPONLY` as it doesn't make Django any more secure, just satisfies some security auditors. - No longer recommend JWT as an authentication method - No longer excuse turning off CSRF to support JWT ## Chapter - FrankenDjango - Added https://daniel.feldroy.com/when-to-use-mongodb-with-django.html - Made Kevin Systrom a co-founder of Instagram instead of CEO ## Chapter - Django Admin - Removed `python_2_unicode_compatible` from the example model ## Chapter - Third Party Packages - Switched from cookiecutter-pypackage to cookiecutter-pylibrary - Switched from `.txt` and `.rst` doc extensions to `.md` - Removed link to Jeff Knupp's excellent article on open sourcing projects as it is unfortunately out of date - For Twine, replaced "non-ssh" with "non-secure" ## Chapter - Bottlenecks - Replaced django-johnnycache and django-cache-machine with django-cacheops - Removed yslow - Change link to Lanyrd search for conference talks to YouTube - Removed link to David Cramer's now defunct blog - Reference Andrew Brook's excellent book, [The Temple of Django Database Performance](https://spellbookpress.com/books/temple-of-django-database-performance/) ## Chapter - Signals - Added `on_delete` argument to foreign key example ## Chapter - User Model - Added methods for differint user types ## Chapter - Testing - Added assertRaisesMessage - Added assertInHTML - Added assertURLEqual - Removed the defunct requestb.in - Added Postman - Added interrogate ## Chapter - Documentation - Removed advising the global installation of Sphinx - Switch to recommending Markdown over reStructuredText - Provide reStructuredText as the older alternative - Remove segment on converting reStructuredText to Markdown - Added tipbox for other markdown doc site renderers - Added interrogate ## Chapter - Bottlenecks - Changed `ATOMIC_REQUESTS` to `False` ## Chapter - Security _Many thanks to our incredibly diligent security reviewers!_ - Fixed borked link to Security Failures Chapter - Added warningbox that Let's Encrypt is the most secure option (in progress of being rewritten) - Added warningbox that not having HTTPS/SSL is inexescusable in 2020 - CSP report submitting is optional per the spec - Made call for SSL very strident - Added cloudflare as a free option for SSL - Mentioned big cloud providers for SSL options - Remove Sasha Romijn's Pony Checkup. Wonderful tool and we thank her for all those years of supporting Django. - ALLOWED_HOSTS and DEBUG=True now throws 400 errors - `IceCreamPayment` model's UUID switched from `db_index=True` to `unique=True` - Discussion of `SECRET_KEY` now states what can actually occur - Added section on not storing unnecessary data which includes credit card, PII, and PHI data - Added missing 'N' to the word 'ever' - Added section on upgrading password hasher to Argon2 - Added section on using SRI when loading static assets from external CDNs ## Chapter - Utilities - Removed `django.utils.six`, we're in Python 3 land now! ## Chapter - Deployment - Removed mention of `mod_python` - Removed reference to very old versions of `mod_wsgi` (3.3 or less) - Removed `--no-site-packages` from virtualenv ## Chapter - Continuous Integration - Removed Python version column from CI-as-a-Service table and replaced with ``operating systems'' - Added Circle CI, GitHub Actions, Azure Pipelines, and Drone ## Chapter - Debugging - Removed defunct django-gargoyle package and added django-flags as the plucky newcomer ## Appendix - Packages - Separated Dependency Management out of Core into own category - Added MkDocs - Added markdown as option to Sphinx - Fixed link to Channels ## Appendix - i18n - Replace `string_concat` with `format_lazy` ## Appendix - Additional resources - Added Simple Better Than Complex, apologies to Vitor we didn't include it at launch - Added classy Django forms - Removed links to our own pages of additional resources ## Appendix - Handling Security Failures - Removed django-maintenaincemode as it hasn't been updated yet to Django 3 - Removed django-maintenancemode-2 as it could create a false sense of security. Per the security reviewer: "that's not a solution, because if your Django installation is compromised, attackers can turn the maintenance mode off again." # Appendix - Security Settings - Made SMTP as SSL mandatory - ALLOWED_HOSTS and DEBUG=True now throws CommandError # Appendix - Websockets with Channels - Removed "Channels Works Better With Python 3.6+". Django 3 makes Python 3.6 or higher mandatory - Fix broken link to Channels' testing documentation ================================================ FILE: code/chapter_00_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class Scoop: def __init__(self): self._is_yummy = True ================================================ FILE: code/chapter_00_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class Scoop: def __init__(self): self._is_yummy = False ================================================ FILE: code/chapter_01_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Stdlib imports from math import sqrt from os.path import abspath # Core Django imports from django.db import models from django.utils.translation import gettext_lazy as _ # Third-party app imports from django_extensions.db.models import TimeStampedModel # Imports from your apps from splits.models import BananaSplit ================================================ FILE: code/chapter_01_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # cones/views.py from django.views.generic import CreateView # Relative imports of the 'cones' package from .models import WaffleCone from .forms import WaffleConeForm # absolute import from the 'core' package from core.views import FoodMixin class WaffleConeCreateView(FoodMixin, CreateView): model = WaffleCone form_class = WaffleConeForm ================================================ FILE: code/chapter_01_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django import forms from django.db import models ================================================ FILE: code/chapter_01_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # ANTI-PATTERN: Don't do this! from django.forms import * from django.db.models import * ================================================ FILE: code/chapter_01_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # ANTI-PATTERN: Don't do this! from django.db.models import CharField from django.forms import CharField ================================================ FILE: code/chapter_01_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.db.models import CharField as ModelCharField from django.forms import CharField as FormCharField ================================================ FILE: code/chapter_01_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ patterns = [ path(route='add/', view=views.add_topping, name='add-topping'), ] ================================================ FILE: code/chapter_01_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ patterns = [ path(route='add/', view=views.add_topping, name='toppings:add_topping'), ] ================================================ FILE: code/chapter_02_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** $ source ~/.virtualenvs/twoscoops/bin/activate ================================================ FILE: code/chapter_02_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** $ workon twoscoops ================================================ FILE: code/chapter_03_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** django-admin startproject mysite cd mysite django-admin startapp my_app ================================================ FILE: code/chapter_03_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** mysite/ ├── manage.py ├── my_app │   ├── __init__.py │   ├── admin.py │   ├── apps.py │   ├── migrations │   │   └── __init__.py │   ├── models.py │   ├── tests.py │   └── views.py └── mysite ├── __init__.py ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py ================================================ FILE: code/chapter_03_example_03.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** / ├── / ├── / ================================================ FILE: code/chapter_03_example_04.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** icecreamratings_project ├── config/ │   ├── settings/ │   ├── __init__.py │   ├── asgi.py │   ├── urls.py │   └── wsgi.py ├── docs/ ├── icecreamratings/ │   ├── media/ # Development only! │   ├── products/ │   ├── profiles/ │   ├── ratings/ │   ├── static/ │   └── templates/ ├── .gitignore ├── Makefile ├── README.md ├── manage.py └── requirements.txt ================================================ FILE: code/chapter_03_example_05.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** ~/projects/icecreamratings_project/ ~/.envs/icecreamratings/ ================================================ FILE: code/chapter_03_example_06.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** c:\projects\icecreamratings_project\ c:\envs\icecreamratings\ ================================================ FILE: code/chapter_03_example_07.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** ~/.virtualenvs/icecreamratings/ ================================================ FILE: code/chapter_03_example_08.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** $ pip freeze ================================================ FILE: code/chapter_03_example_09.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** $ pip freeze > requirements.txt ================================================ FILE: code/chapter_03_example_10.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** cookiecutter https://github.com/pydanny/cookiecutter-django You've downloaded /home/quique/.cookiecutters/cookiecutter-django before. Is it okay to delete and re-download it? [yes]: no Do you want to re-use the existing version? [yes]: yes project_name [My Awesome Project]: icecreamratings project_slug [icecreamratings]: description [Behold My Awesome Project!]: Support your Ice Cream Flavour! author_name [Daniel Roy Greenfeld]: ================================================ FILE: code/chapter_04_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # Common modules scoops/ ├── __init__.py ├── admin.py ├── apps.py ├── forms.py ├── management/ ├── migrations/ ├── models.py ├── templatetags/ ├── tests/ ├── urls.py ├── views.py ================================================ FILE: code/chapter_04_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # uncommon modules scoops/ ├── api/ ├── behaviors.py ├── constants.py ├── context_processors.py ├── decorators.py ├── db/ ├── exceptions.py ├── fields.py ├── factories.py ├── helpers.py ├── managers.py ├── middleware.py ├── schema.py ├── signals.py ├── utils.py ├── viewmixins.py ================================================ FILE: code/chapter_04_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class UserManager(BaseUserManager): """In users/managers.py""" def create_user(self, email=None, password=None, avatar_url=None): user = self.model( email=email, is_active=True, last_login=timezone.now(), registered_at=timezone.now(), avatar_url=avatar_url ) resize_avatar(avatar_url) Ticket.objects.create_ticket(user) return user class TicketManager(models.manager): """In tasks/managers.py""" def create_ticket(self, user: User): ticket = self.model(user=user) send_ticket_to_guest_checkin(ticket) return ticket ================================================ FILE: code/chapter_04_example_04.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # Service layer example scoops/ ├── api/ ├── models.py ├── services.py # Service layer location for business logic ├── selectors.py # Service layer location for queries ├── tests/ ================================================ FILE: code/chapter_04_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # In users/services.py from .models import User from tickets.models import Ticket, send_ticket_to_guest_checkin def create_user(email: str, password: str, avatar_url: str) -> User: user = User( email=email, password=password, avatar_url=avatar_url ) user.full_clean() user.resize_avatar() user.save() ticket = Ticket(user=user) send_ticket_to_guest_checkin(ticket) return user ================================================ FILE: code/chapter_05_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** settings/ ├── __init__.py ├── base.py ├── local.py ├── staging.py ├── test.py ├── production.py ================================================ FILE: code/chapter_05_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** python manage.py shell --settings=config.settings.local ================================================ FILE: code/chapter_05_example_03.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** python manage.py runserver --settings=config.settings.local ================================================ FILE: code/chapter_05_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from .base import * DEBUG = True EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'twoscoops', 'HOST': 'localhost', } } INSTALLED_APPS += ['debug_toolbar', ] ================================================ FILE: code/chapter_05_example_05.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** python manage.py runserver --settings=config.settings.local ================================================ FILE: code/chapter_05_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings/local_pydanny.py from .local import * # Set short cache timeout CACHE_TIMEOUT = 30 ================================================ FILE: code/chapter_05_example_07.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** settings/ __init__.py base.py local_audreyr.py local_pydanny.py local.py staging.py test.py production.py ================================================ FILE: code/chapter_05_example_08.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** export SOME_SECRET_KEY=1c3-cr3am-15-yummy export AUDREY_FREEZER_KEY=y34h-r1ght-d0nt-t0uch-my-1c3-cr34m ================================================ FILE: code/chapter_05_example_09.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** > setx SOME_SECRET_KEY 1c3-cr3am-15-yummy ================================================ FILE: code/chapter_05_example_10.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** [Environment]::SetEnvironmentVariable('SOME_SECRET_KEY', '1c3-cr3am-15-yummy', 'User') [Environment]::SetEnvironmentVariable('AUDREY_FREEZER_KEY', 'y34h-r1ght-d0nt-t0uch-my-1c3-cr34m', 'User') ================================================ FILE: code/chapter_05_example_11.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** [Environment]::SetEnvironmentVariable('SOME_SECRET_KEY', '1c3-cr3am-15-yummy', 'Machine') [Environment]::SetEnvironmentVariable('AUDREY_FREEZER_KEY', 'y34h-r1ght-d0nt-t0uch-my-1c3-cr34m', 'Machine') ================================================ FILE: code/chapter_05_example_12.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** unset SOME_SECRET_KEY unset AUDREY_FREEZER_KEY ================================================ FILE: code/chapter_05_example_13.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** [Environment]::UnsetEnvironmentVariable('SOME_SECRET_KEY', 'User') [Environment]::UnsetEnvironmentVariable('AUDREY_FREEZER_KEY', 'User') ================================================ FILE: code/chapter_05_example_14.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> import os >>> os.environ['SOME_SECRET_KEY'] '1c3-cr3am-15-yummy' ================================================ FILE: code/chapter_05_example_15.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Top of settings/production.py import os SOME_SECRET_KEY = os.environ['SOME_SECRET_KEY'] ================================================ FILE: code/chapter_05_example_16.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings/base.py import os # Normally you should not import ANYTHING from Django directly # into your settings, but ImproperlyConfigured is an exception. from django.core.exceptions import ImproperlyConfigured def get_env_variable(var_name): """Get the environment variable or return exception.""" try: return os.environ[var_name] except KeyError: error_msg = 'Set the {} environment variable'.format(var_name) raise ImproperlyConfigured(error_msg) ================================================ FILE: code/chapter_05_example_17.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ SOME_SECRET_KEY = get_env_variable('SOME_SECRET_KEY') ================================================ FILE: code/chapter_05_example_18.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ django.core.exceptions.ImproperlyConfigured: Set the SOME_SECRET_KEY environment variable. ================================================ FILE: code/chapter_05_example_19.json ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** { "FILENAME": "secrets.json", "SECRET_KEY": "I've got a secret!", "DATABASES_HOST": "127.0.0.1", "PORT": "5432" } ================================================ FILE: code/chapter_05_example_20.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings/base.py import json # Normally you should not import ANYTHING from Django directly # into your settings, but ImproperlyConfigured is an exception. from django.core.exceptions import ImproperlyConfigured # JSON-based secrets module with open('secrets.json') as f: secrets = json.load(f) def get_secret(setting, secrets=secrets): '''Get the secret variable or return explicit exception.''' try: return secrets[setting] except KeyError: error_msg = 'Set the {0} environment variable'.format(setting) raise ImproperlyConfigured(error_msg) SECRET_KEY = get_secret('SECRET_KEY') ================================================ FILE: code/chapter_05_example_21.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** requirements/ ├── base.txt ├── local.txt ├── staging.txt ├── production.txt ================================================ FILE: code/chapter_05_example_22.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** Django==3.2.0 psycopg2-binary==2.8.8 djangorestframework==3.11.0 ================================================ FILE: code/chapter_05_example_23.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** -r base.txt # includes the base.txt requirements file coverage==5.1 django-debug-toolbar==2.2 ================================================ FILE: code/chapter_05_example_24.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** -r base.txt # includes the base.txt requirements file coverage==5.1 ================================================ FILE: code/chapter_05_example_25.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** -r base.txt # includes the base.txt requirements file ================================================ FILE: code/chapter_05_example_26.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** pip install -r requirements/local.txt ================================================ FILE: code/chapter_05_example_27.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** pip install -r requirements/production.txt ================================================ FILE: code/chapter_05_example_28.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings/base.py # Configuring MEDIA_ROOT # DON’T DO THIS! Hardcoded to just one user's preferences MEDIA_ROOT = '/Users/pydanny/twoscoops_project/media' # Configuring STATIC_ROOT # DON’T DO THIS! Hardcoded to just one user's preferences STATIC_ROOT = '/Users/pydanny/twoscoops_project/collected_static' # Configuring STATICFILES_DIRS # DON’T DO THIS! Hardcoded to just one user's preferences STATICFILES_DIRS = ['/Users/pydanny/twoscoops_project/static'] # Configuring TEMPLATES # DON’T DO THIS! Hardcoded to just one user's preferences TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', DIRS: ['/Users/pydanny/twoscoops_project/templates',] }, ] ================================================ FILE: code/chapter_05_example_29.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # At the top of settings/base.py from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent.parent MEDIA_ROOT = BASE_DIR / 'media' STATIC_ROOT = BASE_DIR / 'static_root' STATICFILES_DIRS = [BASE_DIR / 'static'] TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR / 'templates'] }, ] ================================================ FILE: code/chapter_05_example_30.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # At the top of settings/base.py from os.path import abspath, dirname, join def root(*dirs): base_dir = join(dirname(__file__), '..', '..') return abspath(join(base_dir, *dirs)) BASE_DIR = root() MEDIA_ROOT = root('media') STATIC_ROOT = root('static_root') STATICFILES_DIRS = [root('static')] TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [root('templates')], }, ] ================================================ FILE: code/chapter_06_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.db import models class TimeStampedModel(models.Model): """ An abstract base class model that provides self- updating ``created`` and ``modified`` fields. """ created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) class Meta: abstract = True ================================================ FILE: code/chapter_06_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class Meta: abstract = True ================================================ FILE: code/chapter_06_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/models.py from django.db import models from core.models import TimeStampedModel class Flavor(TimeStampedModel): title = models.CharField(max_length=200) ================================================ FILE: code/chapter_06_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.db import migrations, models def add_cones(apps, schema_editor): Scoop = apps.get_model('scoop', 'Scoop') Cone = apps.get_model('cone', 'Cone') for scoop in Scoop.objects.all(): Cone.objects.create( scoop=scoop, style='sugar' ) class Migration(migrations.Migration): initial = True dependencies = [ ('scoop', '0051_auto_20670724'), ] operations = [ migrations.CreateModel( name='Cone', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('style', models.CharField(max_length=10), choices=[('sugar', 'Sugar'), ('waffle', 'Waffle')]), ('scoop', models.OneToOneField(null=True, to='scoop.Scoop' on_delete=django.db.models.deletion.SET_NULL, )), ], ), # RunPython.noop does nothing but allows reverse migrations to occur migrations.RunPython(add_cones, migrations.RunPython.noop) ] ================================================ FILE: code/chapter_06_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # orders/models.py from django.db import models class IceCreamOrder(models.Model): FLAVOR_CHOCOLATE = 'ch' FLAVOR_VANILLA = 'vn' FLAVOR_STRAWBERRY = 'st' FLAVOR_CHUNKY_MUNKY = 'cm' FLAVOR_CHOICES = ( (FLAVOR_CHOCOLATE, 'Chocolate'), (FLAVOR_VANILLA, 'Vanilla'), (FLAVOR_STRAWBERRY, 'Strawberry'), (FLAVOR_CHUNKY_MUNKY, 'Chunky Munky') ) flavor = models.CharField( max_length=2, choices=FLAVOR_CHOICES ) ================================================ FILE: code/chapter_06_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> from orders.models import IceCreamOrder >>> IceCreamOrder.objects.filter(flavor=IceCreamOrder.FLAVOR_CHOCOLATE) [, , ] ================================================ FILE: code/chapter_06_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.db import models class IceCreamOrder(models.Model): class Flavors(models.TextChoices): CHOCOLATE = 'ch', 'Chocolate' VANILLA = 'vn', 'Vanilla' STRAWBERRY = 'st', 'Strawberry' CHUNKY_MUNKY = 'cm', 'Chunky Munky' flavor = models.CharField( max_length=2, choices=Flavors.choices ) ================================================ FILE: code/chapter_06_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> from orders.models import IceCreamOrder >>> IceCreamOrder.objects.filter(flavor=IceCreamOrder.Flavors.CHOCOLATE) [, , ] ================================================ FILE: code/chapter_06_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.db import models from django.utils import timezone class PublishedManager(models.Manager): def published(self, **kwargs): return self.filter(pub_date__lte=timezone.now(), **kwargs) class FlavorReview(models.Model): review = models.CharField(max_length=255) pub_date = models.DateTimeField() # add our custom model manager objects = PublishedManager() ================================================ FILE: code/chapter_06_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> from reviews.models import FlavorReview >>> FlavorReview.objects.count() 35 >>> FlavorReview.objects.published().count() 31 ================================================ FILE: code/chapter_06_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> from reviews.models import FlavorReview >>> FlavorReview.objects.filter().count() 35 >>> FlavorReview.published.filter().count() 31 ================================================ FILE: code/chapter_07_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.core.exceptions import ObjectDoesNotExist from flavors.models import Flavor from store.exceptions import OutOfStock def list_flavor_line_item(sku): try: return Flavor.objects.get(sku=sku, quantity__gt=0) except Flavor.DoesNotExist: msg = 'We are out of {0}'.format(sku) raise OutOfStock(msg) def list_any_line_item(model, sku): try: return model.objects.get(sku=sku, quantity__gt=0) except ObjectDoesNotExist: msg = 'We are out of {0}'.format(sku) raise OutOfStock(msg) ================================================ FILE: code/chapter_07_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from flavors.models import Flavor from store.exceptions import OutOfStock, CorruptedDatabase def list_flavor_line_item(sku): try: return Flavor.objects.get(sku=sku, quantity__gt=0) except Flavor.DoesNotExist: msg = 'We are out of {}'.format(sku) raise OutOfStock(msg) except Flavor.MultipleObjectsReturned: msg = 'Multiple items have SKU {}. Please fix!'.format(sku) raise CorruptedDatabase(msg) ================================================ FILE: code/chapter_07_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Don't do this! from django.db.models import Q from promos.models import Promo def fun_function(name=None): """Find working ice cream promo""" # Too much query chaining makes code go off the screen or page. Not good. return Promo.objects.active().filter(Q(name__startswith=name)|Q(description__icontains=name)).exclude(status='melted') ================================================ FILE: code/chapter_07_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Do this! from django.db.models import Q from promos.models import Promo def fun_function(name=None): """Find working ice cream promo""" results = Promo.objects.active() results = results.filter( Q(name__startswith=name) | Q(description__icontains=name) ) results = results.exclude(status='melted') results = results.select_related('flavors') return results ================================================ FILE: code/chapter_07_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Do this! from django.db.models import Q from promos.models import Promo def fun_function(name=None): """Find working ice cream promo""" qs = (Promo .objects .active() .filter( Q(name__startswith=name) | Q(description__icontains=name) ) .exclude(status='melted') .select_related('flavors') ) return qs ================================================ FILE: code/chapter_07_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ def fun_function(name=None): """Find working ice cream promo""" qs = ( Promo .objects .active() # .filter( # Q(name__startswith=name) | # Q(description__icontains=name) # ) # .exclude(status='melted') # .select_related('flavors') ) breakpoint() return qs ================================================ FILE: code/chapter_07_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Don't do this! from models.customers import Customer customers = [] for customer in Customer.objects.iterator(): if customer.scoops_ordered > customer.store_visits: customers.append(customer) ================================================ FILE: code/chapter_07_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.db.models import F from models.customers import Customer customers = Customer.objects.filter(scoops_ordered__gt=F('store_visits')) ================================================ FILE: code/chapter_07_example_09.sql ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** SELECT * from customers_customer where scoops_ordered > store_visits ================================================ FILE: code/chapter_07_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings/base.py DATABASES = { 'default': { # ... 'ATOMIC_REQUESTS': True, }, } ================================================ FILE: code/chapter_07_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/views.py from django.db import transaction from django.http import HttpResponse from django.shortcuts import get_object_or_404 from django.utils import timezone from .models import Flavor @transaction.non_atomic_requests def posting_flavor_status(request, pk, status): flavor = get_object_or_404(Flavor, pk=pk) # This will execute in autocommit mode (Django's default). flavor.latest_status_change_attempt = timezone.now() flavor.save() with transaction.atomic(): # This code executes inside a transaction. flavor.status = status flavor.latest_status_change_success = timezone.now() flavor.save() return HttpResponse('Hooray') # If the transaction fails, return the appropriate status return HttpResponse('Sadness', status_code=400) ================================================ FILE: code/chapter_08_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Don't do this! from django.urls import path from django.views.generic import DetailView from tastings.models import Tasting urlpatterns = [ path('', DetailView.as_view( model=Tasting, template_name='tastings/detail.html'), name='detail'), path('/results/', DetailView.as_view( model=Tasting, template_name='tastings/results.html'), name='results'), ] ================================================ FILE: code/chapter_08_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.urls import reverse from django.views.generic import ListView, DetailView, UpdateView from .models import Tasting class TasteListView(ListView): model = Tasting class TasteDetailView(DetailView): model = Tasting class TasteResultsView(TasteDetailView): template_name = 'tastings/results.html' class TasteUpdateView(UpdateView): model = Tasting def get_success_url(self): return reverse('tastings:detail', kwargs={'pk': self.object.pk}) ================================================ FILE: code/chapter_08_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.urls import path from . import views urlpatterns = [ path( route='', view=views.TasteListView.as_view(), name='list' ), path( route='/', view=views.TasteDetailView.as_view(), name='detail' ), path( route='/results/', view=views.TasteResultsView.as_view(), name='results' ), path( route='/update/', view=views.TasteUpdateView.as_view(), name='update' ) ] ================================================ FILE: code/chapter_08_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ urlpatterns += [ path('tastings/', include('tastings.urls', namespace='tastings')), ] ================================================ FILE: code/chapter_08_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # tastings/views.py snippet class TasteUpdateView(UpdateView): model = Tasting def get_success_url(self): return reverse('tastings:detail', kwargs={'pk': self.object.pk}) ================================================ FILE: code/chapter_08_example_06.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% extends 'base.html' %} {% block title %}Tastings{% endblock title %} {% block content %} {% endblock content %} ================================================ FILE: code/chapter_08_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # urls.py at root of project urlpatterns += [ path('contact/', include('contactmonger.urls', namespace='contactmonger')), path('report-problem/', include('contactapp.urls', namespace='contactapp')), ] ================================================ FILE: code/chapter_08_example_08.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% extends "base.html" %} {% block title %}Contact{% endblock title %} {% block content %}

Contact Us

Report a Problem

{% endblock content %} ================================================ FILE: code/chapter_08_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Django FBV as a function HttpResponse = view(HttpRequest) # Deciphered into basic math (remember functions from algebra?) y = f(x) # ... and then translated into a CBV example HttpResponse = View.as_view()(HttpRequest) ================================================ FILE: code/chapter_08_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.http import HttpResponse from django.views.generic import View # The simplest FBV def simplest_view(request): # Business logic goes here return HttpResponse('FBV') # The simplest CBV class SimplestView(View): def get(self, request, *args, **kwargs): # Business logic goes here return HttpResponse('CBV') ================================================ FILE: code/chapter_08_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Don't do this! def ice_cream_store_display(request, store_id): store = get_object_or_404(Store, id=store_id) date = timezone.now() return render(request, 'melted_ice_cream_report.html', locals()) ================================================ FILE: code/chapter_08_example_12.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Don't do this! def ice_cream_store_display(request, store_id): store = get_object_or_404(Store, id=store_id) now = timezone.now() return render(request, 'melted_ice_cream_report.html', locals()) ================================================ FILE: code/chapter_08_example_13.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ def ice_cream_store_display(request, store_id): return render( request, 'melted_ice_cream_report.html', { 'store': get_object_or_404(Store, id=store_id), 'now': timezone.now() } ) ================================================ FILE: code/chapter_09_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.core.exceptions import PermissionDenied from django.http import HttpRequest def check_sprinkle_rights(request: HttpRequest) -> HttpRequest: if request.user.can_sprinkle or request.user.is_staff: return request # Return a HTTP 403 back to the user raise PermissionDenied ================================================ FILE: code/chapter_09_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.core.exceptions import PermissionDenied from django.http import HttpRequest, HttpResponse def check_sprinkles(request: HttpRequest) -> HttpRequest: if request.user.can_sprinkle or request.user.is_staff: # By adding this value here it means our display templates # can be more generic. We don't need to have # {% if request.user.can_sprinkle or request.user.is_staff %} # instead just using # {% if request.can_sprinkle %} request.can_sprinkle = True return request # Return a HTTP 403 back to the user raise PermissionDenied ================================================ FILE: code/chapter_09_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # sprinkles/views.py from django.shortcuts import get_object_or_404 from django.shortcuts import render from django.http import HttpRequest, HttpResponse from .models import Sprinkle from .utils import check_sprinkles def sprinkle_list(request: HttpRequest) -> HttpResponse: """Standard list view""" request = check_sprinkles(request) return render(request, "sprinkles/sprinkle_list.html", {"sprinkles": Sprinkle.objects.all()}) def sprinkle_detail(request: HttpRequest, pk: int) -> HttpResponse: """Standard detail view""" request = check_sprinkles(request) sprinkle = get_object_or_404(Sprinkle, pk=pk) return render(request, "sprinkles/sprinkle_detail.html", {"sprinkle": sprinkle}) def sprinkle_preview(request: HttpRequest) -> HttpResponse: """Preview of new sprinkle, but without the check_sprinkles function being used. """ sprinkle = Sprinkle.objects.all() return render(request, "sprinkles/sprinkle_preview.html", {"sprinkle": sprinkle}) ================================================ FILE: code/chapter_09_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.views.generic import DetailView from .models import Sprinkle from .utils import check_sprinkles class SprinkleDetail(DetailView): """Standard detail view""" model = Sprinkle def dispatch(self, request, *args, **kwargs): request = check_sprinkles(request) return super().dispatch(request, *args, **kwargs) ================================================ FILE: code/chapter_09_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ import functools def decorator(view_func): @functools.wraps(view_func) def new_view_func(request, *args, **kwargs): # You can modify the request (HttpRequest) object here. response = view_func(request, *args, **kwargs) # You can modify the response (HttpResponse) object here. return response return new_view_func ================================================ FILE: code/chapter_09_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # sprinkles/decorators.py import functools from . import utils # based off the decorator template from the previous example def check_sprinkles(view_func): """Check if a user can add sprinkles""" @functools.wraps(view_func) def new_view_func(request, *args, **kwargs): # Act on the request object with utils.can_sprinkle() request = utils.can_sprinkle(request) # Call the view function response = view_func(request, *args, **kwargs) # Return the HttpResponse object return response return new_view_func ================================================ FILE: code/chapter_09_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # sprinkles/views.py from django.shortcuts import get_object_or_404, render from .decorators import check_sprinkles from .models import Sprinkle # Attach the decorator to the view @check_sprinkles def sprinkle_detail(request: HttpRequest, pk: int) -> HttpResponse: """Standard detail view""" sprinkle = get_object_or_404(Sprinkle, pk=pk) return render(request, "sprinkles/sprinkle_detail.html", {"sprinkle": sprinkle}) ================================================ FILE: code/chapter_10_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.views.generic import TemplateView class FreshFruitMixin: def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["has_fresh_fruit"] = True return context class FruityFlavorView(FreshFruitMixin, TemplateView): template_name = "fruity_flavor.html" ================================================ FILE: code/chapter_10_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/views.py from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import DetailView from .models import Flavor class FlavorDetailView(LoginRequiredMixin, DetailView): model = Flavor ================================================ FILE: code/chapter_10_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import CreateView from .models import Flavor class FlavorCreateView(LoginRequiredMixin, CreateView): model = Flavor fields = ['title', 'slug', 'scoops_remaining'] def form_valid(self, form): # Do custom logic here return super().form_valid(form) ================================================ FILE: code/chapter_10_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import CreateView from .models import Flavor class FlavorCreateView(LoginRequiredMixin, CreateView): model = Flavor def form_invalid(self, form): # Do custom logic here return super().form_invalid(form) ================================================ FILE: code/chapter_10_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.mixins import LoginRequiredMixin from django.utils.functional import cached_property from django.views.generic import UpdateView, TemplateView from .models import Flavor from .tasks import update_user_who_favorited class FavoriteMixin: @cached_property def likes_and_favorites(self): """Returns a dictionary of likes and favorites""" likes = self.object.likes() favorites = self.object.favorites() return { "likes": likes, "favorites": favorites, "favorites_count": favorites.count(), } class FlavorUpdateView(LoginRequiredMixin, FavoriteMixin, UpdateView): model = Flavor fields = ['title', 'slug', 'scoops_remaining'] def form_valid(self, form): update_user_who_favorited( instance=self.object, favorites=self.likes_and_favorites['favorites'] ) return super().form_valid(form) class FlavorDetailView(LoginRequiredMixin, FavoriteMixin, TemplateView): model = Flavor ================================================ FILE: code/chapter_10_example_06.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# flavors/base.html #} {% extends "base.html" %} {% block likes_and_favorites %}
  • Likes: {{ view.likes_and_favorites.likes }}
  • Favorites: {{ view.likes_and_favorites.favorites_count }}
{% endblock likes_and_favorites %} ================================================ FILE: code/chapter_10_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/models.py from django.db import models from django.urls import reverse class Flavor(models.Model): class Scoops(models.IntegerChoices): SCOOPS_0 = 0 SCOOPS_1 = 1 title = models.CharField(max_length=255) slug = models.SlugField(unique=True) scoops_remaining = models.IntegerField(choices=Scoops.choices, default=Scoops.SCOOPS_0) def get_absolute_url(self): return reverse("flavors:detail", kwargs={"slug": self.slug}) ================================================ FILE: code/chapter_10_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/views.py from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import CreateView, DetailView, UpdateView from .models import Flavor class FlavorCreateView(LoginRequiredMixin, CreateView): model = Flavor fields = ['title', 'slug', 'scoops_remaining'] class FlavorUpdateView(LoginRequiredMixin, UpdateView): model = Flavor fields = ['title', 'slug', 'scoops_remaining'] class FlavorDetailView(DetailView): model = Flavor ================================================ FILE: code/chapter_10_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/views.py from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import CreateView, DetailView, UpdateView from .models import Flavor class FlavorActionMixin: fields = ['title', 'slug', 'scoops_remaining'] @property def success_msg(self): return NotImplemented def form_valid(self, form): messages.info(self.request, self.success_msg) return super().form_valid(form) class FlavorCreateView(LoginRequiredMixin, FlavorActionMixin, CreateView): model = Flavor success_msg = "Flavor created!" class FlavorUpdateView(LoginRequiredMixin, FlavorActionMixin, UpdateView): model = Flavor success_msg = "Flavor updated!" class FlavorDetailView(DetailView): model = Flavor ================================================ FILE: code/chapter_10_example_10.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% if messages %}
    {% for message in messages %}
  • {{ message }}
  • {% endfor %}
{% endif %} ================================================ FILE: code/chapter_10_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.views.generic import ListView from .models import Flavor class FlavorListView(ListView): model = Flavor def get_queryset(self): # Fetch the queryset from the parent get_queryset queryset = super().get_queryset() # Get the q GET parameter q = self.request.GET.get("q") if q: # Return a filtered queryset return queryset.filter(title__icontains=q) # Return the base queryset return queryset ================================================ FILE: code/chapter_10_example_12.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# templates/flavors/_flavor_search.html #} {% comment %} Usage: {% include "flavors/_flavor_search.html" %} {% endcomment %}
================================================ FILE: code/chapter_10_example_13.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import get_object_or_404 from django.shortcuts import render, redirect from django.views.generic import View from .forms import FlavorForm from .models import Flavor class FlavorView(LoginRequiredMixin, View): def get(self, request, *args, **kwargs): # Handles display of the Flavor object flavor = get_object_or_404(Flavor, slug=kwargs['slug']) return render(request, "flavors/flavor_detail.html", {"flavor": flavor} ) def post(self, request, *args, **kwargs): # Handles updates of the Flavor object flavor = get_object_or_404(Flavor, slug=kwargs['slug']) form = FlavorForm(request.POST, instance=flavor) if form.is_valid(): form.save() return redirect("flavors:detail", flavor.slug) ================================================ FILE: code/chapter_10_example_14.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponse from django.shortcuts import get_object_or_404 from django.views.generic import View from .models import Flavor from .reports import make_flavor_pdf class FlavorPDFView(LoginRequiredMixin, View): def get(self, request, *args, **kwargs): # Get the flavor flavor = get_object_or_404(Flavor, slug=kwargs['slug']) # create the response response = HttpResponse(content_type='application/pdf') # generate the PDF stream and attach to the response response = make_flavor_pdf(response, flavor) return response ================================================ FILE: code/chapter_11_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/mixins.py class AsyncViewMixin: async def __call__(self): return super().__call__(self) ================================================ FILE: code/chapter_11_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from asgiref.sync import sync_to_async from django.contrib.auth.mixins import LoginRequiredMixin from django.generic.views import AsyncViewMixin, View from django.shortcuts import get_object_or_404, render, redirect from core.mixins import AsyncViewMixin from .forms import FlavorForm from .models import Flavor class FlavorUpdateView(LoginRequiredMixin, AsyncViewMixin, View): def get(self, request, *args, **kwargs): flavor = get_object_or_404(Flavor, slug=slug) return render(request, "flavor_form.html", {"flavor": Flavor}) def post(self, request, *args, **kwargs): form = FlavorForm(request.POST) if form.is_valid(): sync_to_async(form.save()) else: return render(request, {'form': form}, "flavor_form.html") return redirect("flavor:detail") ================================================ FILE: code/chapter_11_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.mixins import LoginRequiredMixin from django.generic.views import View from django.http import JsonResponse from django.shortcuts import get_object_or_404 import httpx from core.mixins import AsyncViewMixin class InvokeMicroserviceView(LoginRequiredMixin, AsyncViewMixin, View): def post(self, request, *args, **kwargs): % try: % async with httpx.AsyncClient() as client: % response = await client.get(PROMO_SERVICE_URL) % if response.status_code == httpx.codes.OK: % context["promo"] = response.json() % response = await client.get(RECCO_SERVICE_URL) % if response.status_code == httpx.codes.OK: % context["recco"] = response.json() % except httpx.RequestError as exc: % print(f"An error occurred while requesting {exc.request.url!r}.") return JsonResponse({}) ================================================ FILE: code/chapter_12_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import CreateView, UpdateView from .models import Flavor class FlavorCreateView(LoginRequiredMixin, CreateView): model = Flavor fields = ['title', 'slug', 'scoops_remaining'] class FlavorUpdateView(LoginRequiredMixin, UpdateView): model = Flavor fields = ['title', 'slug', 'scoops_remaining'] ================================================ FILE: code/chapter_12_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/validators.py from django.core.exceptions import ValidationError def validate_tasty(value): """Raise a ValidationError if the value doesn't start with the word 'Tasty'. """ if not value.startswith('Tasty'): msg = 'Must start with Tasty' raise ValidationError(msg) ================================================ FILE: code/chapter_12_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/models.py from django.db import models from .validators import validate_tasty class TastyTitleAbstractModel(models.Model): title = models.CharField(max_length=255, validators=[validate_tasty]) class Meta: abstract = True ================================================ FILE: code/chapter_12_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/models.py from django.db import models from django.urls import reverse from core.models import TastyTitleAbstractModel class Flavor(TastyTitleAbstractModel): slug = models.SlugField() scoops_remaining = models.IntegerField(default=0) def get_absolute_url(self): return reverse('flavors:detail', kwargs={'slug': self.slug}) ================================================ FILE: code/chapter_12_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/forms.py from django import forms from .models import Flavor from core.validators import validate_tasty class FlavorForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['title'].validators.append(validate_tasty) self.fields['slug'].validators.append(validate_tasty) class Meta: model = Flavor fields = ['title', 'slug'] ================================================ FILE: code/chapter_12_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/views.py from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import CreateView, DetailView, UpdateView from .models import Flavor from .forms import FlavorForm class FlavorActionMixin: model = Flavor fields = ['title', 'slug', 'scoops_remaining'] @property def success_msg(self): return NotImplemented def form_valid(self, form): messages.info(self.request, self.success_msg) return super().form_valid(form) class FlavorCreateView(LoginRequiredMixin, FlavorActionMixin, CreateView): success_msg = 'created' # Explicitly attach the FlavorForm class form_class = FlavorForm class FlavorUpdateView(LoginRequiredMixin, FlavorActionMixin, UpdateView): success_msg = 'updated' # Explicitly attach the FlavorForm class form_class = FlavorForm class FlavorDetailView(DetailView): model = Flavor ================================================ FILE: code/chapter_12_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/forms.py from django import forms from flavors.models import Flavor class IceCreamOrderForm(forms.Form): """Normally done with forms.ModelForm. But we use forms.Form here to demonstrate that these sorts of techniques work on every type of form. """ slug = forms.ChoiceField(label='Flavor') toppings = forms.CharField() def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # We dynamically set the choices here rather than # in the flavor field definition. Setting them in # the field definition means status updates won't # be reflected in the form without server restarts. self.fields['slug'].choices = [ (x.slug, x.title) for x in Flavor.objects.all() ] # NOTE: We could filter by whether or not a flavor # has any scoops, but this is an example of # how to use clean_slug, not filter(). def clean_slug(self): slug = self.cleaned_data['slug'] if Flavor.objects.get(slug=slug).scoops_remaining <= 0: msg = 'Sorry, we are out of that flavor.' raise forms.ValidationError(msg) return slug ================================================ FILE: code/chapter_12_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # attach this code to the previous example (12.7) def clean(self): cleaned_data = super().clean() slug = cleaned_data.get('slug', '') toppings = cleaned_data.get('toppings', '') # Silly "too much chocolate" validation example in_slug = 'chocolate' in slug.lower() in_toppings = 'chocolate' in toppings.lower() if in_slug and in_toppings: msg = 'Your order has too much chocolate.' raise forms.ValidationError(msg) return cleaned_data ================================================ FILE: code/chapter_12_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/models.py from django.db import models from django.urls import reverse class IceCreamStore(models.Model): title = models.CharField(max_length=100) block_address = models.TextField() phone = models.CharField(max_length=20, blank=True) description = models.TextField(blank=True) def get_absolute_url(self): return reverse('stores:store_detail', kwargs={'pk': self.pk}) ================================================ FILE: code/chapter_12_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/forms.py from django import forms from .models import IceCreamStore class IceCreamStoreUpdateForm(forms.ModelForm): # Don't do this! Duplication of the model field! phone = forms.CharField(required=True) # Don't do this! Duplication of the model field! description = forms.TextField(required=True) class Meta: model = IceCreamStore fields = '__all__' ================================================ FILE: code/chapter_12_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/forms.py # Call phone and description from the self.fields dict-like object from django import forms from .models import IceCreamStore class IceCreamStoreUpdateForm(forms.ModelForm): class Meta: model = IceCreamStore fields = ['phone', 'description'] def __init__(self, *args, **kwargs): # Call the original __init__ method before assigning # field overloads super().__init__(*args, **kwargs) self.fields['phone'].required = True self.fields['description'].required = True ================================================ FILE: code/chapter_12_example_12.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/forms.py from django import forms from .models import IceCreamStore class IceCreamStoreCreateForm(forms.ModelForm): class Meta: model = IceCreamStore fields = ['title', 'block_address', ] class IceCreamStoreUpdateForm(IceCreamStoreCreateForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['phone'].required = True self.fields['description'].required = True class Meta(IceCreamStoreCreateForm.Meta): # show all the fields! fields = ['title', 'block_address', 'phone', 'description', ] ================================================ FILE: code/chapter_12_example_13.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/views from django.views.generic import CreateView, UpdateView from .forms import IceCreamStoreCreateForm, IceCreamStoreUpdateForm from .models import IceCreamStore class IceCreamCreateView(CreateView): model = IceCreamStore form_class = IceCreamStoreCreateForm class IceCreamUpdateView(UpdateView): model = IceCreamStore form_class = IceCreamStoreUpdateForm ================================================ FILE: code/chapter_12_example_14.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/views.py class TitleSearchMixin: def get_queryset(self): # Fetch the queryset from the parent's get_queryset queryset = super().get_queryset() # Get the q GET parameter q = self.request.GET.get('q') if q: # return a filtered queryset return queryset.filter(title__icontains=q) # No q is specified so we return queryset return queryset ================================================ FILE: code/chapter_12_example_15.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # add to flavors/views.py from django.views.generic import ListView from .models import Flavor from core.views import TitleSearchMixin class FlavorListView(TitleSearchMixin, ListView): model = Flavor ================================================ FILE: code/chapter_12_example_16.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # add to stores/views.py from django.views.generic import ListView from .models import Store from core.views import TitleSearchMixin class IceCreamStoreListView(TitleSearchMixin, ListView): model = Store ================================================ FILE: code/chapter_12_example_17.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# form to go into stores/store_list.html template #}
================================================ FILE: code/chapter_12_example_18.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# form to go into flavors/flavor_list.html template #}
================================================ FILE: code/chapter_13_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ import csv from django.utils.six import StringIO from .models import Purchase def add_csv_purchases(rows): rows = StringIO.StringIO(rows) records_added = 0 # Generate a dict per row, with the first CSV row being the keys for row in csv.DictReader(rows, delimiter=','): # DON'T DO THIS: Tossing unvalidated data into your model. Purchase.objects.create(**row) records_added += 1 return records_added ================================================ FILE: code/chapter_13_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ import csv from django.utils.six import StringIO from django import forms from .models import Purchase, Seller class PurchaseForm(forms.ModelForm): class Meta: model = Purchase fields = '__all__' def clean_seller(self): seller = self.cleaned_data['seller'] try: Seller.objects.get(name=seller) except Seller.DoesNotExist: msg = '{0} does not exist in purchase #{1}.'.format( seller, self.cleaned_data['purchase_number'] ) raise forms.ValidationError(msg) return seller def add_csv_purchases(rows): rows = StringIO.StringIO(rows) records_added = 0 errors = [] # Generate a dict per row, with the first CSV row being the keys. for row in csv.DictReader(rows, delimiter=','): # Bind the row data to the PurchaseForm. form = PurchaseForm(row) # Check to see if the row data is valid. if form.is_valid(): # Row data is valid so save the record. form.save() records_added += 1 else: errors.append(form.errors) return records_added, errors ================================================ FILE: code/chapter_13_example_03.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """
================================================ FILE: code/chapter_13_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django import forms from .models import Taster class TasterForm(forms.ModelForm): class Meta: model = Taster fields = ['name', 'dreams', 'aspirations'] def __init__(self, *args, **kwargs): # set the user as an attribute of the form self.user = kwargs.pop('user') super().__init__(*args, **kwargs) ================================================ FILE: code/chapter_13_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import UpdateView from .forms import TasterForm from .models import Taster class TasterUpdateView(LoginRequiredMixin, UpdateView): model = Taster form_class = TasterForm success_url = '/someplace/' def get_form_kwargs(self): """This method is what injects forms with keyword arguments.""" # grab the current set of form #kwargs kwargs = super().get_form_kwargs() # Update the kwargs with the user_id kwargs['user'] = self.request.user return kwargs ================================================ FILE: code/chapter_13_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/models.py from django.db import models class ModelFormFailureHistory(models.Model): form_data = models.TextField() model_data = models.TextField() ================================================ FILE: code/chapter_13_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/views.py import json from django.contrib import messages from django.core import serializers from core.models import ModelFormFailureHistory class FlavorActionMixin: @property def success_msg(self): return NotImplemented def form_valid(self, form): messages.info(self.request, self.success_msg) return super().form_valid(form) def form_invalid(self, form): """Save invalid form and model data for later reference.""" form_data = json.dumps(form.cleaned_data) # Serialize the form.instance model_data = serializers.serialize('json', [form.instance]) # Strip away leading and ending bracket leaving only a dict model_data = model_data[1:-1] ModelFormFailureHistory.objects.create( form_data=form_data, model_data=model_data ) return super().form_invalid(form) ================================================ FILE: code/chapter_13_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django import forms class IceCreamReviewForm(forms.Form): # Rest of tester form goes here ... def clean(self): cleaned_data = super().clean() flavor = cleaned_data.get('flavor') age = cleaned_data.get('age') if flavor == 'coffee' and age < 3: # Record errors that will be displayed later. msg = 'Coffee Ice Cream is not for Babies.' self.add_error('flavor', msg) self.add_error('age', msg) # Always return the full collection of cleaned data. return cleaned_data ================================================ FILE: code/chapter_13_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings.py FORM_RENDERER = 'django.forms.renderers.TemplatesSetting' INSTALLED_APPS = [ ... 'django.forms', ... ] ================================================ FILE: code/chapter_13_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/widgets.py from django.forms.widgets import TextInput class IceCreamFlavorInput(TextInput): """Ice cream flavors must always end with 'Ice Cream'""" def get_context(self, name, value, attrs): context = super().get_context(name, value, attrs) value = context['widget']['value'] if not value.strip().lower().endswith('ice cream'): context['widget']['value'] = '{} Ice Cream'.format(value) return context ================================================ FILE: code/chapter_14_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** templates/ ├── base.html ├── ... (other sitewide templates in here) ├── freezers/ │   ├── ("freezers" app templates in here) ================================================ FILE: code/chapter_14_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** freezers/ ├── templates/ │   ├── freezers/ │   │   ├── ... ("freezers" app templates in here) templates/ ├── base.html ├── ... (other sitewide templates in here) ================================================ FILE: code/chapter_14_example_03.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** templates/ ├── base.html ├── dashboard.html # extends base.html ├── profiles/ │ ├── profile_detail.html # extends base.html │ ├── profile_form.html # extends base.html ================================================ FILE: code/chapter_14_example_04.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** templates/ base.html dashboard.html # extends base.html profiles/ base_profiles.html # extends base.html profile_detail.html # extends base_profiles.html profile_form.html # extends base_profiles.html ================================================ FILE: code/chapter_14_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # vouchers/models.py from django.db import models from .managers import VoucherManager class Voucher(models.Model): """Vouchers for free pints of ice cream.""" name = models.CharField(max_length=100) email = models.EmailField() address = models.TextField() birth_date = models.DateField(blank=True) sent = models.DateTimeField(null=True, default=None) redeemed = models.DateTimeField(null=True, default=None) objects = VoucherManager() ================================================ FILE: code/chapter_14_example_06.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# templates/vouchers/ages.html #} {% extends "base.html" %} {% block content %} {% for age_bracket in age_brackets %} {% endfor %}
Age Bracket Number of Vouchers Issued
{{ age_bracket.title }} {{ age_bracket.count }}
{% endblock content %} ================================================ FILE: code/chapter_14_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # vouchers/managers.py from django.db import models from django.utils import timezone from dateutil.relativedelta import relativedelta class VoucherManager(models.Manager): def age_breakdown(self): """Returns a dict of age brackets/counts.""" age_brackets = [] delta = timezone.now() - relativedelta(years=18) count = self.model.objects.filter(birth_date__gt=delta).count() age_brackets.append( {'title': '0-17', 'count': count} ) count = self.model.objects.filter(birth_date__lte=delta).count() age_brackets.append( {'title': '18+', 'count': count} ) return age_brackets ================================================ FILE: code/chapter_14_example_08.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """

Greenfelds Who Want Ice Cream

    {% for voucher in voucher_list %} {# Don't do this: conditional filtering in templates #} {% if 'greenfeld' in voucher.name.lower %}
  • {{ voucher.name }}
  • {% endif %} {% endfor %}

Roys Who Want Ice Cream

    {% for voucher in voucher_list %} {# Don't do this: conditional filtering in templates #} {% if 'roy' in voucher.name.lower %}
  • {{ voucher.name }}
  • {% endif %} {% endfor %}
================================================ FILE: code/chapter_14_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # vouchers/views.py from django.views.generic import TemplateView from .models import Voucher class GreenfeldRoyView(TemplateView): template_name = 'vouchers/views_conditional.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['greenfelds'] = \ Voucher.objects.filter(name__icontains='greenfeld') context['roys'] = Voucher.objects.filter(name__icontains='roy') return context ================================================ FILE: code/chapter_14_example_10.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """

Greenfelds Who Want Ice Cream

    {% for voucher in greenfelds %}
  • {{ voucher.name }}
  • {% endfor %}

Roys Who Want Ice Cream

    {% for voucher in roys %}
  • {{ voucher.name }}
  • {% endfor %}
================================================ FILE: code/chapter_14_example_11.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# list generated via User.objects.all() #}

Ice Cream Fans and their favorite flavors.

    {% for user in user_list %}
  • {{ user.name }}: {# DON'T DO THIS: Generated implicit query per user #} {{ user.flavor.title }} {# DON'T DO THIS: Second implicit query per user!!! #} {{ user.flavor.scoops_remaining }}
  • {% endfor %}
================================================ FILE: code/chapter_14_example_12.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% comment %} List generated via User.objects.all().select_related('flavor') {% endcomment %}

Ice Cream Fans and their favorite flavors.

    {% for user in user_list %}
  • {{ user.name }}: {{ user.flavor.title }} {{ user.flavor.scoops_remaining }}
  • {% endfor %}
================================================ FILE: code/chapter_14_example_13.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% comment %}Don't do this! This code bunches everything together to generate pretty HTML. {% endcomment %} {% if list_type=='unordered' %}
    {% else %}
      {% endif %}{% for syrup in syrup_list %}
    1. {% syrup.title %}
    2. {% endfor %}{% if list_type=='unordered' %}
{% else %} {% endif %} ================================================ FILE: code/chapter_14_example_14.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# Use indentation/comments to ensure code quality #} {# start of list elements #} {% if list_type=='unordered' %}
    {% else %}
      {% endif %} {% for syrup in syrup_list %}
    1. {% syrup.title %}
    2. {% endfor %} {# end of list elements #} {% if list_type=='unordered' %}
{% else %} {% endif %} ================================================ FILE: code/chapter_14_example_15.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# simple base.html #} {% load static %} {% block title %}Two Scoops of Django{% endblock title %} {% block stylesheets %} {% endblock stylesheets %}
{% block content %}

Two Scoops

{% endblock content %}
================================================ FILE: code/chapter_14_example_16.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% extends "base.html" %} {% load static %} {% block title %}About Audrey and Daniel{% endblock title %} {% block stylesheets %} {{ block.super }} {% endblock stylesheets %} {% block content %} {{ block.super }}

About Audrey and Daniel

They enjoy eating ice cream

{% endblock content %} ================================================ FILE: code/chapter_14_example_17.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ About Audrey and Daniel

Two Scoops

About Audrey and Daniel

They enjoy eating ice cream

================================================ FILE: code/chapter_14_example_18.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% extends "base.html" %} {% block stylesheets %} {{ block.super }} {# this brings in project.css #} {% endblock stylesheets %} ================================================ FILE: code/chapter_14_example_19.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% extends "base.html" %} {% block stylesheets %} {% comment %} By not using {{ block.super }}, this block overrides the stylesheet block of base.html {% endcomment %} {% endblock stylesheets %} ================================================ FILE: code/chapter_14_example_20.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% extends "base.html" %} {% comment %} By not using {% block stylesheets %}, this template inherits the stylesheets block from the base.html parent, in this case the default project.css link. {% endcomment %} ================================================ FILE: code/chapter_14_example_21.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {# templates/toppings/topping_list.html #} {# Using implicit names, good for code reuse #}
    {% for object in object_list %}
  1. {{ object }}
  2. {% endfor %}
{# Using explicit names, good for object specific code #}
    {% for topping in topping_list %}
  1. {{ topping }}
  2. {% endfor %}
================================================ FILE: code/chapter_14_example_22.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ ================================================ FILE: code/chapter_14_example_23.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ ================================================ FILE: code/chapter_14_example_24.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings/local.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'APP_DIRS': True, 'OPTIONS': { 'string_if_invalid': 'INVALID EXPRESSION: %s' } }, ] ================================================ FILE: code/chapter_15_example_01.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% extends "base.html" %} {% load flavors_tags %} ================================================ FILE: code/chapter_15_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings/base.py TEMPLATES = [ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'OPTIONS': { # Don't do this! # It's an evil anti-pattern! 'builtins': ['flavors.templatetags.flavors_tags'], }, ] ================================================ FILE: code/chapter_16_example_01.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """
================================================ FILE: code/chapter_16_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/jinja2.py from django.contrib.staticfiles.storage import staticfiles_storage from django.template import defaultfilters from django.urls import reverse from jinja2 import Environment def environment(**options): env = Environment(**options) env.globals.update({ 'static': staticfiles_storage.url, 'url': reverse, 'dj': defaultfilters }) return env ================================================ FILE: code/chapter_16_example_03.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% for purchase in purchase_list %} {{ purchase.title }} {{ dj.date(purchase.created, 'SHORT_DATE_FORMAT') }}{{ dj.floatformat(purchase.amount, 2) }} {% endfor %}
================================================ FILE: code/chapter_16_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/mixins.py from django.template import defaultfilters class DjFilterMixin: dj = defaultfilters ================================================ FILE: code/chapter_16_example_05.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {% for purchase in purchase_list %} {{ purchase.title }} {{ view.dj.date(purchase.created, 'SHORT_DATE_FORMAT') }}{{ view.dj.floatformat(purchase.amount, 2) }} {% endfor %}
================================================ FILE: code/chapter_16_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/jinja2.py from jinja2 import Environment import random def environment(**options): env = Environment(**options) env.globals.update({ # Runs only on the first template load! The three displays below # will all present the same number. # {{ random_once }} {{ random_once }} {{ random_once }} 'random_once': random.randint(1, 5) # Can be called repeated as a function in templates. Each call # returns a random number: # {{ random() }} {{ random() }} {{ random() }} 'random': lambda: random.randint(1, 5), }) return env ================================================ FILE: code/chapter_17_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAdminUser', ), } ================================================ FILE: code/chapter_17_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/models.py import uuid as uuid_lib from django.db import models from django.urls import reverse class Flavor(models.Model): title = models.CharField(max_length=255) slug = models.SlugField(unique=True) # Used to find the web URL uuid = models.UUIDField( # Used by the API to look up the record db_index=True, default=uuid_lib.uuid4, editable=False) scoops_remaining = models.IntegerField(default=0) def get_absolute_url(self): return reverse('flavors:detail', kwargs={'slug': self.slug}) ================================================ FILE: code/chapter_17_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/api/serializers.py from rest_framework import serializers from ..models import Flavor class FlavorSerializer(serializers.ModelSerializer): class Meta: model = Flavor fields = ['title', 'slug', 'uuid', 'scoops_remaining'] ================================================ FILE: code/chapter_17_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/api/views.py from rest_framework.generics import ( ListCreateAPIView, RetrieveUpdateDestroyAPIView ) from rest_framework.permissions import IsAuthenticated from ..models import Flavor from .serializers import FlavorSerializer class FlavorListCreateAPIView(ListCreateAPIView): queryset = Flavor.objects.all() permission_classes = (IsAuthenticated, ) serializer_class = FlavorSerializer lookup_field = 'uuid' # Don't use Flavor.id! class FlavorRetrieveUpdateDestroyAPIView(RetrieveUpdateDestroyAPIView): queryset = Flavor.objects.all() permission_classes = (IsAuthenticated, ) serializer_class = FlavorSerializer lookup_field = 'uuid' # Don't use Flavor.id! ================================================ FILE: code/chapter_17_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/urls.py from django.urls import path from flavors.api import views urlpatterns = [ # /flavors/api/ path( route='api/', view=views.FlavorListCreateAPIView.as_view(), name='flavor_rest_api' ), # /flavors/api/:uuid/ path( route='api//', view=views.FlavorRetrieveUpdateDestroyAPIView.as_view(), name='flavor_rest_api' ) ] ================================================ FILE: code/chapter_17_example_06.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** flavors/api/ flavors/api/:uuid/ ================================================ FILE: code/chapter_17_example_07.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** flavors/ ├── api/ │   ├── __init__.py │   ├── authentication.py │   ├── parsers.py │   ├── permissions.py │   ├── renderers.py │   ├── serializers.py │   ├── validators.py │   ├── views.py │   ├── viewsets.py ================================================ FILE: code/chapter_17_example_08.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** flavors/ ├── api/ │   ├── __init__.py │   ├── ... other modules here │   ├── views │   │   ├── __init__.py │   │   ├── flavor.py │   │   ├── ingredient.py ================================================ FILE: code/chapter_17_example_09.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** api/flavors/ # GET, POST api/flavors/:uuid/ # GET, PUT, DELETE api/users/ # GET, POST api/users/:uuid/ # GET, PUT, DELETE ================================================ FILE: code/chapter_17_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/api_urls.py """Called from the project root's urls.py URLConf thus: path('api/', include('core.api_urls', namespace='api')), """ from django.urls import path from flavors.api import views as flavor_views from users.api import views as user_views urlpatterns = [ # {% url 'api:flavors' %} path( route='flavors/', view=flavor_views.FlavorCreateReadView.as_view(), name='flavors' ), # {% url 'api:flavors' flavor.uuid %} path( route='flavors//', view=flavor_views.FlavorReadUpdateDeleteView.as_view(), name='flavors' ), # {% url 'api:users' %} path( route='users/', view=user_views.UserCreateReadView.as_view(), name='users' ), # {% url 'api:users' user.uuid %} path( route='users//', view=user_views.UserReadUpdateDeleteView.as_view(), name='users' ), ] ================================================ FILE: code/chapter_17_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # sundaes/api/views.py from django.shortcuts import get_object_or_404 from rest_framework.response import Response from rest_framework.views import APIView from ..models import Sundae, Syrup from .serializers import SundaeSerializer, SyrupSerializer class PourSyrupOnSundaeView(APIView): """View dedicated to adding syrup to sundaes""" def post(self, request, *args, **kwargs): # Process pouring of syrup here, # Limit each type of syrup to just one pour # Max pours is 3 per sundae sundae = get_object_or_404(Sundae, uuid=request.data['uuid']) try: sundae.add_syrup(request.data['syrup']) except Sundae.TooManySyrups: msg = "Sundae already maxed out for syrups" return Response({'message': msg}, status_code=400) except Syrup.DoesNotExist msg = "{} does not exist".format(request.data['syrup']) return Response({'message': msg}, status_code=404) return Response(SundaeSerializer(sundae).data) def get(self, request, *args, **kwargs) # Get list of syrups already poured onto the sundae sundae = get_object_or_404(Sundae, uuid=request.data['uuid']) syrups = [SyrupSerializer(x).data for x in sundae.syrup_set.all()] return Response(syrups) ================================================ FILE: code/chapter_17_example_12.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** /sundae/ # GET, POST /sundae/:uuid/ # PUT, DELETE /sundae/:uuid/syrup/ # GET, POST /syrup/ # GET, POST /syrup/:uuid/ # PUT, DELETE ================================================ FILE: code/chapter_17_example_13.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** /api/cones/ # GET, POST /api/cones/:uuid/ # PUT, DELETE /api/scoops/ # GET, POST /api/scoops/:uuid/ # PUT, DELETE ================================================ FILE: code/chapter_17_example_14.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** /api/cones/ # GET, POST /api/cones/:uuid/ # PUT, DELETE /api/cones/:uuid/scoops/ # GET, POST /api/cones/:uuid/scoops/:uuid/ # PUT, DELETE /api/scoops/ # GET, POST /api/scoops/:uuid/ # PUT, DELETE ================================================ FILE: code/chapter_17_example_15.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/apiv1_shutdown.py from django.http import HttpResponseGone apiv1_gone_msg = """APIv1 was removed on April 2, 2017. Please switch to APIv2: """ def apiv1_gone(request): return HttpResponseGone(apiv1_gone_msg) ================================================ FILE: code/chapter_17_example_16.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class FlavorApiView(LoginRequiredMixin,View): def post(self, request, *args, **kwargs): # logic goes here return JsonResponse({}) def get(self, request, *args, **kwargs): # logic goes here return JsonResponse({}) def put(self, request, *args, **kwargs): # logic goes here return JsonResponse({}) def delete(self, request, *args, **kwargs): # logic goes here return JsonResponse({}) ================================================ FILE: code/chapter_18_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** config/ ├── schema.py # imports forms & models from flavors app ├── settings/ ├── urls.py flavors/ ├── __init__.py ├── app.py ├── forms.py ├── models.py ================================================ FILE: code/chapter_18_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** config/ ├── schema.py # imports queries/mutations from flavors app ├── settings/ ├── urls.py flavors/ ├── __init__.py ├── app.py ├── api/ │   ├── queries.py # imports models │   ├── mutations.py # imports forms and models ├── forms.py ├── models.py ================================================ FILE: code/chapter_19_example_01.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ {{ page_data|json_script:"page-data" }} ================================================ FILE: code/chapter_21_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.db import models class IceCreamBar(models.Model): name = models.CharField(max_length=100) shell = models.CharField(max_length=100) filling = models.CharField(max_length=100) has_stick = models.BooleanField(default=True) def __str__(self): return self.name ================================================ FILE: code/chapter_21_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> IceCreamBar.objects.all() [, , ] ================================================ FILE: code/chapter_21_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib import admin from .models import IceCreamBar @admin.register(IceCreamBar) class IceCreamBarModelAdmin(admin.ModelAdmin): list_display = ('name', 'shell', 'filling') ================================================ FILE: code/chapter_21_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # icecreambars/admin.py from django.contrib import admin from django.urls import reverse, NoReverseMatch from django.utils.html import format_html from .models import IceCreamBar @admin.register(IceCreamBar) class IceCreamBarModelAdmin(admin.ModelAdmin): list_display = ('name', 'shell', 'filling') readonly_fields = ('show_url',) def show_url(self, instance): url = reverse('icecreambars:ice_cream_bar_detail', kwargs={'pk': instance.pk}) response = format_html("""{0}""", url) return response show_url.short_description = 'Ice Cream Bar URL' ================================================ FILE: code/chapter_22_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Stock user model definition >>> from django.contrib.auth import get_user_model >>> get_user_model() # When the project has a custom user model definition >>> from django.contrib.auth import get_user_model >>> get_user_model() ================================================ FILE: code/chapter_22_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.conf import settings from django.db import models class IceCreamStore(models.Model): owner = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL) title = models.CharField(max_length=255) ================================================ FILE: code/chapter_22_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # DON'T DO THIS! from django.contrib.auth import get_user_model from django.db import models class IceCreamStore(models.Model): # This following line tends to create import loops. owner = models.OneToOneField(get_user_model(), on_delete=models.SET_NULL) title = models.CharField(max_length=255) ================================================ FILE: code/chapter_22_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # profiles/models.py from django.contrib.auth.models import AbstractUser from django.db import models class KarmaUser(AbstractUser): karma = models.PositiveIntegerField(verbose_name='karma', default=0, blank=True) ================================================ FILE: code/chapter_22_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ AUTH_USER_MODEL = 'profiles.KarmaUser' ================================================ FILE: code/chapter_22_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # profiles/models.py from django.conf import settings from django.db import models from flavors.models import Flavor class EaterProfile(models.Model): # Default user profile # If you do this you need to either have a post_save signal or # redirect to a profile_edit view on initial login. user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL) favorite_ice_cream = models.ForeignKey(Flavor, null=True, blank=True) class ScooperProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL) scoops_scooped = models.IntegerField(default=0) class InventorProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL) flavors_invented = models.ManyToManyField(Flavor, null=True, blank=True) ================================================ FILE: code/chapter_22_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class User(AbstractUser): class Types(models.TextChoices): EATER = "EATER", "Eater" SCOOPER = "SCOOPER", "Scooper" INVENTOR = "INVENTOR", "Inventor" # What type of user are we? type = models.CharField( _("Type"), max_length=50, choices=Types.choices, default=Types.EATER ) ================================================ FILE: code/chapter_22_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class User(AbstractUser): class Types(models.TextChoices): EATER = "EATER", "Eater" SCOOPER = "SCOOPER", "Scooper" INVENTOR = "INVENTOR", "Inventor" # Ensures that creating new users through proxy models works base_type = Types.EATER # What type of user are we? type = models.CharField( _("Type"), max_length=50, choices=Types.choices, default=Types.EATER ) # ... def save(self, *args, **kwargs): # If a new user, set the user's type based off the # base_type property if not self.pk: self.type = self.base_type return super().save(*args, **kwargs) ================================================ FILE: code/chapter_22_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class InventorManager(BaseUserManager): def get_queryset(self, *args, **kwargs): results = super().get_queryset(*args, **kwargs) return results.filter(type=User.Types.INVENTOR) class Inventor(User): # This sets the user type to INVENTOR during record creation base_type = User.Types.INVENTOR # Ensures queries on the Inventor model return only Inventors objects = InventorManager() # Setting proxy to "True" means a table WILL NOT be created # for this record class Meta: proxy = True # Only inventors get to invent new flavors! def invent(self): # Magical custom logic goes Here return "Delicious!" ================================================ FILE: code/chapter_22_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> from users.models import User, Inventor >>> User.objects.count() # Over 300 million users! 323482357 >>> Inventor.objects.count() # But only 3 inventors 3 >>> # Calling someone as both a User and an Inventor >>> user = User.objects.get(username='uma') >>> user >>> inventor = Inventor.objects.get(username='uma') >>> inventor >>> # Calling a method that's only for inventors >>> user.invent() AttributeError >>> inventor.invent() 'Delicious' ================================================ FILE: code/chapter_22_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> User.objects.filter(type=User.Types.INVENTOR) >>> Inventor.objects.filter() # Our preference ================================================ FILE: code/chapter_22_example_12.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ class Inventor(User): # ... objects = InventorManager() class Meta: proxy = True @property def extra(self): return self.inventorprofile class Scooper(User): # ... objects = ScooperManager() class Meta: proxy = True @property def extra(self): return self.scooperprofile class Eater(User): # ... objects = EaterManager() class Meta: proxy = True @property def extra(self): return self.eaterprofile ================================================ FILE: code/chapter_23_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** Django==1.11 coverage==4.3.4 django-extensions==1.7.6 django-braces==1.11 ================================================ FILE: code/chapter_23_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** -e git+https://github.com/erly-adptr/py-junk.git#egg=py-jnk ================================================ FILE: code/chapter_23_example_03.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # DON'T DO THIS! # requirements for django-blarg Django==3.0 requests==2.11.0 ================================================ FILE: code/chapter_23_example_04.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # requirements.txt for the mythical web site 'icecreamratings.com' Django==3.1 requests==2.13.0 django−blarg==1.0 # Note that unlike the django−blarg library , we explicitly pin # the requirements so we have total control over the environment ================================================ FILE: code/chapter_23_example_05.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # requirements for django-blarg Django>=3.1,<3.0 requests>=2.13.0,<=3.0.0 ================================================ FILE: code/chapter_23_example_06.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # Only if you haven't installed cookiecutter yet \$ pip install cookiecutter # Creating a Django Package from scratch \$ cookiecutter https://github.com/pydanny/cookiecutter-djangopackage.git # Creating a Python Package from scratch \$ cookiecutter https://github.com//ionelmc/cookiecutter-pylibrary.git ================================================ FILE: code/chapter_23_example_07.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** pip install twine python setup.py sdist twine upload dist/* ================================================ FILE: code/chapter_23_example_08.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** pip install wheel ================================================ FILE: code/chapter_23_example_09.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** python setup.py bdist_wheel twine upload dist/* ================================================ FILE: code/chapter_23_example_10.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # setup.cfg [wheel] universal = 1 ================================================ FILE: code/chapter_23_example_11.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** git tag -a v1.4 -m "my version 1.4" git push origin v1.4 ================================================ FILE: code/chapter_24_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** popsicles/ __init__.py admin.py forms.py models.py tests/ __init__.py test_forms.py test_models.py test_views.py views.py ================================================ FILE: code/chapter_24_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/tests/test_api.py import json from django.test import TestCase from django.urls import reverse from flavors.models import Flavor class FlavorAPITests(TestCase): def setUp(self): Flavor.objects.get_or_create(title='A Title', slug='a-slug') def test_list(self): url = reverse('flavors:flavor_object_api') response = self.client.get(url) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(len(data), 1) ================================================ FILE: code/chapter_24_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # flavors/tests/test_api.py import json from django.test import TestCase from django.urls import reverse from flavors.models import Flavor class DjangoRestFrameworkTests(TestCase): def setUp(self): Flavor.objects.get_or_create(title='title1', slug='slug1') Flavor.objects.get_or_create(title='title2', slug='slug2') self.create_read_url = reverse('flavors:flavor_rest_api') self.read_update_delete_url = \ reverse('flavors:flavor_rest_api', kwargs={'slug': 'slug1'}) def test_list(self): response = self.client.get(self.create_read_url) # Are both titles in the content? self.assertContains(response, 'title1') self.assertContains(response, 'title2') def test_detail(self): response = self.client.get(self.read_update_delete_url) data = json.loads(response.content) content = {'id': 1, 'title': 'title1', 'slug': 'slug1', 'scoops_remaining': 0} self.assertEquals(data, content) def test_create(self): post = {'title': 'title3', 'slug': 'slug3'} response = self.client.post(self.create_read_url, post) data = json.loads(response.content) self.assertEquals(response.status_code, 201) content = {'id': 3, 'title': 'title3', 'slug': 'slug3', 'scoops_remaining': 0} self.assertEquals(data, content) self.assertEquals(Flavor.objects.count(), 3) def test_delete(self): response = self.client.delete(self.read_update_delete_url) self.assertEquals(response.status_code, 204) self.assertEquals(Flavor.objects.count(), 1) ================================================ FILE: code/chapter_24_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.contrib.auth.models import AnonymousUser from django.contrib.sessions.middleware import SessionMiddleware from django.test import TestCase, RequestFactory from .views import cheese_flavors def add_middleware_to_request(request, middleware_class): middleware = middleware_class() middleware.process_request(request) return request def add_middleware_to_response(request, middleware_class): middleware = middleware_class() middleware.process_response(request) return request class SavoryIceCreamTest(TestCase): def setUp(self): # Every test needs access to the request factory. self.factory = RequestFactory() def test_cheese_flavors(self): request = self.factory.get('/cheesy/broccoli/') request.user = AnonymousUser() # Annotate the request object with a session request = add_middleware_to_request(request, SessionMiddleware) request.session.save() # process and test the request response = cheese_flavors(request) self.assertContains(response, 'bleah!') ================================================ FILE: code/chapter_24_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from unittest import mock, TestCase import icecreamapi from flavors.exceptions import CantListFlavors from flavors.utils import list_flavors_sorted class TestIceCreamSorting(TestCase): # Set up monkeypatch of icecreamapi.get_flavors() @mock.patch.object(icecreamapi, 'get_flavors') def test_flavor_sort(self, get_flavors): # Instructs icecreamapi.get_flavors() to return an unordered list. get_flavors.return_value = ['chocolate', 'vanilla', 'strawberry', ] # list_flavors_sorted() calls the icecreamapi.get_flavors() # function. Since we've monkeypatched the function, it will always # return ['chocolate', 'strawberry', 'vanilla', ]. Which the. # list_flavors_sorted() will sort alphabetically flavors = list_flavors_sorted() self.assertEqual( flavors, ['chocolate', 'strawberry', 'vanilla', ] ) ================================================ FILE: code/chapter_24_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ @mock.patch.object(icecreamapi, 'get_flavors') def test_flavor_sort_failure(self, get_flavors): # Instructs icecreamapi.get_flavors() to throw a FlavorError. get_flavors.side_effect = icecreamapi.FlavorError() # list_flavors_sorted() catches the icecreamapi.FlavorError() # and passes on a CantListFlavors exception. with self.assertRaises(CantListFlavors): list_flavors_sorted() ================================================ FILE: code/chapter_24_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ @mock.patch.object(requests, 'get') def test_request_failure(self, get): """Test if the target site is inaccessible.""" get.side_effect = requests.exception.ConnectionError() with self.assertRaises(CantListFlavors): list_flavors_sorted() @mock.patch.object(requests, 'get') def test_request_failure_ssl(self, get): """Test if we can handle SSL problems elegantly.""" get.side_effect = requests.exception.SSLError() with self.assertRaises(CantListFlavors): list_flavors_sorted() ================================================ FILE: code/chapter_24_example_08.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** $ coverage run manage.py test --settings=twoscoops.settings.test ================================================ FILE: code/chapter_24_example_09.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** Creating test database for alias "default"... .. ----------------------------------------------- Ran 2 tests in 0.008s OK Destroying test database for alias "default"... ================================================ FILE: code/chapter_24_example_10.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** $ coverage html --omit="admin.py" ================================================ FILE: code/chapter_24_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # test_models.py from pytest import raises from cones.models import Cone def test_good_choice(): assert Cone.objects.filter(type='sugar').count() == 1 def test_bad_cone_choice(): with raises(Cone.DoesNotExist): Cone.objects.get(type='spaghetti') ================================================ FILE: code/chapter_25_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** # H1 Header **emphasis (bold/strong)** *italics* _underline_ link: [Two Scoops Press](https://twoscoopspress.com) Note: The secret to links in Markdown is think of it like a function call. The link in the paranthesis is the value being called. ## Section Header #. An enumerated list item #. Second item - First bullet - Second bullet - Indented Bullet - Note carriage return and indents Literal code block: ``` python def like(): print("I like Ice Cream") for i in range(10): like() ``` JavaScript colored code block: ``` js console.log("Don't use alert()"); ``` ================================================ FILE: code/chapter_25_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** \$ # To convert a ReStructuredText document to GitHub-Flavored Markdown \$ pandoc -t gfm README.rst -o README.md \$ # To convert a Markdown document to ReStructuredText \$ pandoc -f gfm README.md -o README.rst ================================================ FILE: code/chapter_28_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True ================================================ FILE: code/chapter_28_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** Strict-Transport-Security: max-age=31536000; includeSubDomains ================================================ FILE: code/chapter_28_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django import forms class SpecialForm(forms.Form): my_secret = forms.CharField( widget=forms.TextInput(attrs={'autocomplete': 'off'})) ================================================ FILE: code/chapter_28_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django import forms class SecretInPublicForm(forms.Form): my_secret = forms.CharField(widget=forms.PasswordInput()) ================================================ FILE: code/chapter_28_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/models.py from django.conf import settings from django.db import models class Store(models.Model): title = models.CharField(max_length=255) slug = models.SlugField() owner = models.ForeignKey(settings.AUTH_USER_MODEL) # Assume 10 more fields that cover address and contact info. ================================================ FILE: code/chapter_28_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # DON'T DO THIS! from django import forms from .models import Store class StoreForm(forms.ModelForm): class Meta: model = Store # DON'T DO THIS: Implicit definition of fields. # Too easy to make mistakes! excludes = ("pk", "slug", "modified", "created", "owner") ================================================ FILE: code/chapter_28_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django import forms from .models import Store class StoreForm(forms.ModelForm): class Meta: model = Store # Explicitly specifying the fields we want fields = ( "title", "address_1", "address_2", "email", "usstate", "postal_code", "city", ) ================================================ FILE: code/chapter_28_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/models.py from django.conf import settings from django.db import models class Store(models.Model): title = models.CharField(max_length=255) slug = models.SlugField() owner = models.ForeignKey(settings.AUTH_USER_MODEL) co_owners = models.ManyToManyField(settings.AUTH_USER_MODEL) # Assume 10 more fields that cover address and contact info. ================================================ FILE: code/chapter_28_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ import uuid from django.db import models class IceCreamPayment(models.Model): uuid = models.UUIDField( unique=True, default=uuid.uuid4, editable=False) def __str__(self): return str(self.pk) ================================================ FILE: code/chapter_28_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> from payments import IceCreamPayment >>> payment = IceCreamPayment() >>> IceCreamPayment.objects.get(id=payment.id) >>> payment.uuid UUID('0b0fb68e-5b06-44af-845a-01b6df5e0967') >>> IceCreamPayment.objects.get(uuid=payment.uuid) ================================================ FILE: code/chapter_28_example_11.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ ================================================ FILE: code/chapter_28_example_12.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ ================================================ FILE: code/chapter_29_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Taken directly from core Django code. # Used here to illustrate an example only, so don't # copy this into your project. logger.error('Internal Server Error: %s', request.path, exc_info=exc_info, extra={ 'status_code': 500, 'request': request } ) ================================================ FILE: code/chapter_29_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Taken directly from core Django code. # Used here to illustrate an example only, so don't # copy this into your project. logger.warning('Forbidden (%s): %s', REASON_NO_CSRF_COOKIE, request.path, extra={ 'status_code': 403, 'request': request, } ) ================================================ FILE: code/chapter_29_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.views.generic import TemplateView from .helpers import pint_counter class PintView(TemplateView): def get_context_data(self, *args, **kwargs): context = super().get_context_data(**kwargs) pints_remaining = pint_counter() print(f'Only {pints_remaining} pints of ice cream left.') return context ================================================ FILE: code/chapter_29_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ import logging from django.views.generic import TemplateView from .helpers import pint_counter logger = logging.getLogger(__name__) class PintView(TemplateView): def get_context_data(self, *args, **kwargs): context = super().get_context_data(**kwargs) pints_remaining = pint_counter() logger.debug('Only %d pints of ice cream left.' % pints_remaining) return context ================================================ FILE: code/chapter_29_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ import logging import requests logger = logging.getLogger(__name__) def get_additional_data(): try: r = requests.get('http://example.com/something-optional/') except requests.HTTPError as e: logger.exception(e) logger.debug('Could not get additional data', exc_info=True) return None return r ================================================ FILE: code/chapter_29_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # You can place this snippet at the top # of models.py, views.py, or any other # file where you need to log. import logging logger = logging.getLogger(__name__) ================================================ FILE: code/chapter_30_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # events/managers.py from django.db import models class EventManager(models.Manager): def create_event(self, title, start, end, creator): event = self.model(title=title, start=start, end=end, creator=creator) event.save() event.notify_admins() return event ================================================ FILE: code/chapter_30_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # events/models.py from textwrap import dedent from django.conf import settings from django.core.mail import mail_admins from django.db import models from model_utils.models import TimeStampedModel from .managers import EventManager class Event(TimeStampedModel): class Status(models.IntegerChoices): STATUS_UNREVIEWED = 0, "Unreviewed" STATUS_REVIEWED = 1, "Reviewed" title = models.CharField(max_length=100) start = models.DateTimeField() end = models.DateTimeField() status = models.IntegerField(choices=Status.choices, default=Status.STATUS_UNREVIEWED) creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) objects = EventManager() def notify_admins(self): # create the subject and message subject = "{user} submitted a new event!".format( user=self.creator.get_full_name()) message = dedentf"""TITLE: {self.title} START: {self.start} END: {self.end}""") # Send to the admins! mail_admins(subject=subject, message=message, fail_silently=False) ================================================ FILE: code/chapter_30_example_03.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** >>> from django.contrib.auth import get_user_model >>> from django.utils import timezone >>> from events.models import Event >>> user = get_user_model().objects.get(username="audreyr") >>> now = timezone.now() >>> event = Event.objects.create_event( ... title="International Ice Cream Tasting Competition", ... start=now, ... end=now, ... user=user ... ) ================================================ FILE: code/chapter_31_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** core/ __init__.py managers.py # contains the custom model manager(s) models.py views.py # Contains the custom view mixin(s) ================================================ FILE: code/chapter_31_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from core.managers import PublishedManager from core.views import IceCreamMixin ================================================ FILE: code/chapter_31_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> from django.utils.text import slugify >>> slugify('straße') # German 'strae' ================================================ FILE: code/chapter_31_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ >>> slugify('straße', allow_unicode=True) # Again with German 'straße' ================================================ FILE: code/chapter_31_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/utils.py from django.core.exceptions import ObjectDoesNotExist class BorkedObject: loaded = False def generic_load_tool(model, pk): try: instance = model.objects.get(pk=pk) except ObjectDoesNotExist: return BorkedObject() instance.loaded = True return instance ================================================ FILE: code/chapter_31_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/utils.py from django.core.exceptions import MultipleObjectsReturned from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import PermissionDenied def get_object_or_403(model, **kwargs): try: return model.objects.get(**kwargs) except ObjectDoesNotExist: raise PermissionDenied except MultipleObjectsReturned: raise PermissionDenied ================================================ FILE: code/chapter_31_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/calc.py def finance_data_adjudication(store, sales, issues): if store.something_not_right: msg = 'Something is not right. Please contact the support team.' raise PermissionDenied(msg) # Continue on to perform other logic. ================================================ FILE: code/chapter_31_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # urls.py # This demonstrates the use of a custom permission denied view. The default # view is django.views.defaults.permission_denied handler403 = 'core.views.permission_denied_view' ================================================ FILE: code/chapter_31_example_09.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # serializer_example.py from django.core.serializers import get_serializer from favorites.models import Favorite # Get and instantiate the serializer class # The 'json' can be replaced with 'python' or 'xml'. # If you have pyyaml installed, you can replace it with # 'pyyaml' JSONSerializer = get_serializer('json') serializer = JSONSerializer() favs = Favorite.objects.filter()[:5] # Serialize model data serialized_data = serializer.serialize(favs) # save the serialized data for use in the next example with open('data.json', 'w') as f: f.write(serialized_data) ================================================ FILE: code/chapter_31_example_10.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # deserializer_example.py from django.core.serializers import get_serializer from favorites.models import Favorite # Get and instantiate the serializer class # The 'json' can be replaced with 'python' or 'xml'. # If you have pyyaml installed, you can replace it with # 'pyyaml' JSONSerializer = get_serializer('json') serializer = JSONSerializer() # open the serialized data file with open('data.txt') as f: serialized_data = f.read() # deserialize model data into a generator object # we'll call 'python data' python_data = serializer.deserialize(serialized_data) # iterate through the python_data for element in python_data: # Prints 'django.core.serializers.base.DeserializedObject' print(type(element)) # Elements have an 'object' that are literally instantiated # model instances (in this case, favorites.models.Favorite) print( element.object.pk, element.object.created ) ================================================ FILE: code/chapter_31_example_11.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # json_encoding_example.py import json from django.core.serializers.json import DjangoJSONEncoder from django.utils import timezone data = {'date': timezone.now()} # If you don't add the DjangoJSONEncoder class then # the json library will throw a TypeError. json_data = json.dumps(data, cls=DjangoJSONEncoder) print(json_data) ================================================ FILE: code/chapter_35_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** twoscoopspress\$ python discounts/manage.py runserver 8001 Starting development server at http://127.0.0.1:8001/ Quit the server with CONTROL-C. Internal Server Error: / Traceback (most recent call last): File "/.envs/oc/lib/python.7/site-packages/django/core/handlers/base.py", line 132, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/.envs/oc/lib/python.7/site-packages/django/utils/decorators.py", line 145, in inner return func(*args, **kwargs) TypeError: __init__() takes exactly 1 argument (2 given) ================================================ FILE: code/chapter_35_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Forgetting the 'as_view()' method path('', HomePageView, name='home'), ================================================ FILE: code/chapter_35_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ path('', HomePageView.as_view(), name='home'), ================================================ FILE: code/chapter_35_example_04.html ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ ================================================ FILE: code/chapter_35_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/views.py from django.shortcuts import render, redirect, get_object_or_404 from django.views.generic import View from stores.forms import UploadFileForm from stores.models import Store def upload_file(request, pk): """Simple FBV example""" store = get_object_or_404(Store, pk=pk) if request.method == 'POST': # Don't forget to add request.FILES! form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): store.handle_uploaded_file(request.FILES['file']) return redirect(store) else: form = UploadFileForm() return render(request, 'upload.html', {'form': form, 'store': store}) ================================================ FILE: code/chapter_35_example_06.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # stores/views.py from django.shortcuts import render, redirect, get_object_or_404 from django.views.generic import View from stores.forms import UploadFileForm from stores.models import Store class UploadFile(View): """Simple CBV example""" def get_object(self): return get_object_or_404(Store, pk=self.kwargs['pk']) def post(self, request, *args, **kwargs): store = self.get_object() form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): store.handle_uploaded_file(request.FILES['file']) return redirect(store) return redirect('stores:file_upload', pk=pk) def get(self, request, *args, **kwargs): store = self.get_object() form = UploadFileForm() return render( request, 'upload.html', {'form': form, 'store': store}) ================================================ FILE: code/chapter_35_example_07.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # core/middleware.py import sys from django.views.debug import technical_500_response class UserBasedExceptionMiddleware: def process_exception(self, request, exception): if request.user.is_superuser: return technical_500_response(request, *sys.exc_info()) ================================================ FILE: code/chapter_35_example_08.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # settings.py ALLOWED_HOSTS = [ '.djangopackages.org', '.djangopackages.com', ] ================================================ FILE: code/chapter_39_example_01.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** python -c "import django; print(django.get_version())" ================================================ FILE: code/chapter_39_example_02.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** 3.x, where "x" is the version of Django you are using ================================================ FILE: code/chapter_39_example_03.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** $ virtualenv testenv $ source testenv/bin/activate ================================================ FILE: code/chapter_39_example_04.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** C:\code\> virtualenv testenv C:\code\> testenv\Scripts\activate ================================================ FILE: code/chapter_39_example_05.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** (testenv) \$ ================================================ FILE: code/chapter_39_example_06.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** (testenv) > ================================================ FILE: code/chapter_39_example_07.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** python -c "import django; print(django.get_version())" ================================================ FILE: code/chapter_39_example_08.txt ================================================ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. ******************** (testenv) \$ pip install Django==3.2 ================================================ FILE: code/chapter_41_example_01.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.utils.translation import gettext as _ print(_('We like gelato.')) ================================================ FILE: code/chapter_41_example_02.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # DON'T DO THIS! # Skipping the rest of imports for the sake of brevity class FlavorActionMixin: @property def action(self): msg = '{0} is missing action.'.format(self.__class__) raise NotImplementedError(msg) def form_valid(self, form): msg = 'Flavor {0}!'.format(self.action) messages.info(self.request, msg) return super().form_valid(form) # Snipping the rest of this module for the sake of brevity ================================================ FILE: code/chapter_41_example_03.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # DON'T DO THIS! from django.utils.translations import gettext as _ # Skipping the rest of this module for the sake of brevity def form_valid(self, form): # This generates a useless translation object. msg = _('Flavor {0}!'.format(self.action)) messages.info(self.request, msg) return super().form_valid(form) # Skipping the rest of this module for the sake of brevity ================================================ FILE: code/chapter_41_example_04.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ # Skipping the rest of imports for the sake of brevity from django.utils.translation import gettext as _ class FlavorActionMixin: @property def success_msg(self): return NotImplemented class FlavorCreateView(LoginRequiredMixin, FlavorActionMixin, CreateView): model = Flavor # Slightly longer but more meaningful dialogue success_msg = _('Flavor created!') # Skipping the rest of this module for the sake of brevity ================================================ FILE: code/chapter_41_example_05.py ================================================ """ Using This Code Example ========================= The code examples provided are provided by Daniel and Audrey Roy Greenfeld of feldroy.com to help you reference Two Scoops of Django: Best Practices for Django 3.x. Code samples follow PEP-0008, with exceptions made for the purposes of improving book formatting. Example code is provided "as is", and is not intended to be, and should not be considered or labeled as "tutorial code". Permissions ============ In general, you may use the code we've provided with this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code or using it in commercial distributions. Examples: * Writing a program that uses several chunks of code from this course does not require permission. * Selling or distributing a digital package from material taken from this book does require permission. * Answering a question by citing this book and quoting example code does not require permission. * Incorporating a significant amount of example code from this book into your product's documentation does require permission. Attributions usually include the title, author, publisher and an ISBN. For example, "Two Scoops of Django: Best Practices for Django 3.x, by Daniel and Audrey Roy Greenfeld. Copyright 2020 Feldroy.com." If you feel your use of code examples falls outside fair use of the permission given here, please contact us at hi@feldroy.com. """ from django.utils.translation import gettext as _ class FlavorActionMixin: @property def success_msg(self): return NotImplemented class FlavorCreateView(LoginRequiredMixin, FlavorActionMixin, CreateView): model = Flavor # Example combining strings part_one = _('Flavor created! ') part_two = _("Let's go try it!") success_msg = part_one + part_two # Skipping the rest of this module for the sake of brevity