Django Design Patterns and Best Practices
eBook - ePub

Django Design Patterns and Best Practices

Arun Ravindran

Share book
  1. 222 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Django Design Patterns and Best Practices

Arun Ravindran

Book details
Book preview
Table of contents
Citations

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Django Design Patterns and Best Practices an online PDF/ePUB?
Yes, you can access Django Design Patterns and Best Practices by Arun Ravindran in PDF and/or ePUB format, as well as other popular books in Computer Science & Web Development. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781783986644

Django Design Patterns and Best Practices


Table of Contents

Django Design Patterns and Best Practices
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Django and Patterns
Why Django?
The story of Django
A framework is born
Removing the magic
Django keeps getting better
How does Django work?
What is a Pattern?
Gang of Four Patterns
Is Django MVC?
Fowler's Patterns
Are there more patterns?
Patterns in this book
Criticism of Patterns
How to use Patterns
Best practices
Python Zen and Django's design philosophy
Summary
2. Application Design
How to gather requirements
Are you a story teller?
HTML mockups
Designing the application
Dividing a project into Apps
Reuse or roll-your-own?
My app sandbox
Which packages made it?
Before starting the project
SuperBook – your mission, should you choose to accept it
Why Python 3?
Starting the project
Summary
3. Models
M is bigger than V and C
The model hunt
Splitting models.py into multiple files
Structural patterns
Patterns – normalized models
Problem details
Solution details
Three steps of normalization
First normal form (1NF)
Second normal form or 2NF
Third normal form or 3NF
Django models
Performance and denormalization
Should we always normalize?
Pattern – model mixins
Problem details
Solution details
Model mixins
Pattern – user profiles
Problem details
Solution details
Signals
Admin
Multiple profile types
Pattern – service objects
Problem details
Solution details
Retrieval patterns
Pattern – property field
Problem details
Solution details
Cached properties
Pattern – custom model managers
Problem details
Solution details
Set operations on QuerySets
Chaining multiple QuerySets
Migrations
Summary
4. Views and URLs
A view from the top
Views got classier
Class-based generic views
View mixins
Order of mixins
Decorators
View patterns
Pattern – access controlled views
Problem details
Solution details
Pattern – context enhancers
Problem details
Solution details
Pattern – services
Problem details
Solution details
Designing URLs
URL anatomy
What happens in urls.py?
The URL pattern syntax
Mnemonic – parents question pink action-figures
Names and namespaces
Pattern order
URL pattern styles
Departmental store URLs
RESTful URLs
Summary
5. Templates
Understanding Django's template language features
Variables
Attributes
Filters
Tags
Philosophy – don't invent a programming language
Organizing templates
Support for other template languages
Using Bootstrap
But they all look the same!
Template patterns
Pattern – template inheritance tree
Problem details
Solution details
Pattern – the active link
Problem details
Solution details
A template-only solution
Custom tags
Summary
6. Admin Interface
Using the admin interface
Enhancing models for the admin
Not everyone should be an admin
Admin interface customizations
Changing the heading
Changing the base and stylesheets
Adding a Rich Text Editor for WYSIWYG editing
Bootstrap-themed admin
Complete overhauls
Protecting the admin
Pattern – feature flags
Problem details
Solution details
Summary
7. Forms
How forms work
Forms in Django
Why does data need cleaning?
Displaying forms
Time to be crisp
Understanding CSRF
Form processing with Class-based views
Form patterns
Pattern – dynamic form generation
Problem details
Solution details
Pattern – user-based forms
Problem details
Solution details
Pattern – multiple form actions per view
Problem details
Solution details
Separate views for separate actions
Same view for separate actions
Pattern – CRUD views
Problem details
Solution details
Summary
8. Dealing with Legacy Code
Finding the Django version
Activating the virtual environment
Where are the files? This is not PHP
Starting with urls.py
Jumping around the code
Understanding the code base
Creating the big picture
Incremental change or a full rewrite?
Write tests before making any changes
Step-by-step process to writing tests
Legacy databases
Summary
9. Testing and Debugging
Why write tests?
Test-driven development
Writing a test case
The assert method
Writing better test cases
Mocking
Pattern – test fixtures and factories
Problem details
Solution details
Learning more about testing
Debugging
Django debug page
A better debug page
The print function
Logging
The Django Debug Toolbar
The Python debugger pdb
Other debuggers
Debugging Django templates
Summary
10. Security
Cross-site scripting (XSS)
Why are your cookies valuable?
How Django helps
Where Django might not help
Cross-Site Request Forgery (CSRF)
How Django helps
Where Django might not help
SQL injection
How Django helps
Where Django might not help
Clickjacking
How Django helps
Shell injection
How Django helps
And the list goes on
A handy security checklist
Summary
11. Production-ready
Production environment
Choosing a web stack
Components of a stack
Hosting
Platform as a service
Virtual private servers
Other hosting approaches
Deployment tools
Fabric
Typical deployment steps
Configuration management
Monitoring
Performance
Frontend performance
Backend performance
Templates
Database
Caching
Cached session backend
Caching frameworks
Caching patterns
Summary
A. Python 2 versus Python 3
But I still use Python 2.7!
Python 3
Python 3 for Djangonauts
Change all the __unicode__ methods into __str__
All classes inherit from the object class
Calling super() is easier
Relative imports must be explicit
HttpRequest and HttpResponse have str and bytes types
Exception syntax changes and improvements
Standard library reor...

Table of contents