Django Design Patterns and Best Practices

Book description

Easily build maintainable websites with powerful and relevant Django design patterns

In Detail

Learning how to write better Django code to build more maintainable websites either takes a lot of experience or familiarity with various design patterns. Filled with several idiomatic Django patterns, Django Design Patterns and Best Practices accelerates your journey into the world of web development.

Discover a set of common design problems, each tackling aspects of Django including model design and Views, and learn several recommended solutions with useful code examples and illustrations. You'll also get to grips with the current best practices in the latest versions of Django and Python. Creating a successful web application involves much more than Django, so advanced topics including REST, testing, debugging, security, and deployment are also explored in detail.

What You Will Learn

  • Understand common design patterns that can help you write better Django code
  • Discover the latest best practices and idioms in this rapidly evolving framework
  • Deep dive into various aspects of Django from Models and Views to testing and deployment
  • Get to grips with difficult topics such as dealing with legacy code and debugging
  • Familiarize yourself with the various nuances of web development such as browser attacks and databases
  • Reduce the maintenance burden with well-tested, cleaner code
  • Show your work to the world by learning about hosting, deploying, and securing your Django projects

Table of contents

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

Product information

  • Title: Django Design Patterns and Best Practices
  • Author(s): Arun Ravindran
  • Release date: March 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781783986644