Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Django Design Patterns and Best Practices. - Second Edition

You're reading from  Django Design Patterns and Best Practices. - Second Edition

Product type Book
Published in May 2018
Publisher Packt
ISBN-13 9781788831345
Pages 282 pages
Edition 2nd Edition
Languages
Author (1):
Arun Ravindran Arun Ravindran
Profile icon Arun Ravindran
Toc

Table of Contents (21) Chapters close

Title Page
Copyright and Credits
PacktPub.com
Contributors
Preface
1. Django and Patterns 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 1. Python 2 Versus Python 3 2. Other Books You May Enjoy Index

How does Django work?


To truly appreciate Django, you will need to peek under the hood and see the various moving parts inside. This can be both enlightening and overwhelming. If you are already familiar with the following information, you might want to skip this section:

How web requests are processed in a typical Django application

The preceding diagram shows the simplified journey of a web request from a visitor's browser to your Django application and back. The numbered paths are as follows:

  1. The browser sends the request (essentially, a string of bytes) to your web server.
  2. Your web server (say, Nginx) hands over the request to a Web Server Gateway Interface (WSGI) server (say, uWSGI) or directly serves a file (say, a CSS file) from the filesystem.
  3. Unlike a web server, WSGI servers can run Python applications. The request populates a Python dictionary called environ and, optionally, passes through several layers of middleware, ultimately reaching your Django application.
  4. URLconf (URL configuration) module contained in the urls.py of your project selects a view to handle the request based on the requested URL. The request has turned into HttpRequest, a Python object.
  1. The selected view typically does one or more of the following things:

a. Talks to a database via the models

b. Renders HTML or any other formatted response using templates

c. Returns a plain text response (not shown)

d. Raises an exception

  1. The HttpResponse object gets rendered into a string, as it leaves the Django application.
  2. A beautifully rendered web page is seen in your user's browser.

Though certain details are omitted, this representation should help you appreciate Django's high-level architecture. It also shows the roles played by the key components, such as models, views, and templates. Many of Django's components are based on several well-known design patterns.

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 $15.99/month. Cancel anytime}