Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Django Design Patterns and Best Practices

You're reading from   Django Design Patterns and Best Practices Industry-standard web development techniques and solutions using Python

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781788831345
Length 282 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Arun Ravindran Arun Ravindran
Author Profile Icon Arun Ravindran
Arun Ravindran
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Django and Patterns FREE CHAPTER 2. Application Design 3. Models 4. Views and URLs 5. Templates 6. Admin Interface 7. Forms 8. Working Asynchronously 9. Creating APIs 10. Dealing with Legacy Code 11. Testing and Debugging 12. Security 13. Production-Ready 14. Python 2 Versus Python 3 15. Other Books You May Enjoy

Patterns in this book

This book will cover Django-specific design and architecture patterns, which would be useful to a Django developer. This is how each pattern will be presented:

Pattern name

The heading is the pattern name. If it is a well-known pattern, the commonly used name is used; otherwise, a terse, self-descriptive name has been chosen. Names are important, as they help in building the pattern vocabulary. All patterns will have the following parts:

  • Problem: This briefly mentions the problem
  • Solution: This summarizes the proposed solution(s)
  • Problem Details: This elaborates the context of the problem and possibly gives an example
  • Solution Details: This explains the solution(s) in general terms and provides a sample Django implementation

Criticism of patterns

Despite their near universal usage, patterns have their share of criticism too. The most common arguments against them are as follows:

  • Patterns compensate for the missing language features: Peter Norvig found that 16 of the 23 patterns in design patterns were invisible or simpler in dynamic languages such as Lisp or Python. For instance, as functions are already objects in Python, it would be unnecessary to create separate classes to implement strategy patterns.
  • Patterns repeat best practices: Many patterns are essentially formalizations of best practices, such as separation of concerns, and could seem redundant.
  • Patterns can lead to over-engineering: Implementing the pattern might be less efficient and excessive compared to a simpler solution.

How to use patterns

Although some of the previous criticisms are quite valid, they are based on how patterns are misused. Here is some advice that can help you understand how best to use design patterns:

  • Patterns are best used to communicate that you are following a well-understood design approach
  • Don't implement a pattern if your language supports a direct solution
  • Don't try to retrofit everything in terms of patterns
  • Use a pattern only if it is the most elegant solution in your context
  • Don't be afraid to create new patterns

Python Zen and Django's design philosophy

Generally, the Python community uses the term Pythonic to describe a piece of idiomatic code. It typically refers to the principles laid out in The Zen of Python. Written like a poem, it is extremely useful to describe such a vague concept.

Try entering import this in a Python prompt to view The Zen of Python.

Furthermore, Django developers have crisply documented their design philosophies while designing the framework at https://docs.djangoproject.com/en/dev/misc/design-philosophies/.

While the document describes the thought process behind how Django was designed, it is also useful for developers using Django to build applications. Certain principles such as Don't Repeat Yourself (DRY), loose coupling, and tight cohesion can help you write more maintainable and idiomatic Django applications.

Django or Python best practices suggested by this book would be formatted in the following manner:

Use BASE_DIR in settings.py and avoid hardcoding directory names.
You have been reading a chapter from
Django Design Patterns and Best Practices - Second Edition
Published in: May 2018
Publisher: Packt
ISBN-13: 9781788831345
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image