Django

Definition

Django is a high-level, “batteries-included” Python web framework built around an ORM, a request/response middleware pipeline, and a choice of function- or class-based views.


Core Ideas

Views: FBV vs CBV

Django started with function-based views (FBVs). Class-based views (CBVs) were added to templatize common functionality so you don’t rewrite the same boilerplate repeatedly. FBVs are explicit and easy to read; CBVs favor reuse via inheritance and mixins.

The ORM (QuerySets)

  • QuerySets are lazy — no database hit until the results are actually iterated; use caching to reuse them and boost performance.
  • Q objects (django.db.models.Q) encapsulate a collection of keyword-argument lookups, enabling complex OR/AND/NOT filters that plain kwargs can’t express.
  • Filters build querysets; drop to .raw() for raw SQL when needed.

Reading request parameters

  • Form/body paramsrequest.POST
  • URL path params → view arguments, e.g. def get(request, some_param)
  • Query-string paramsrequest.GET
  • Headers via the request meta.

Middleware pipeline

A request passes through every middleware’s process_request, then routing runs and process_view fires (after all request phases, once the URL matches and the view + args are resolved, but before the view executes). The response then passes back through every process_response. Additional hooks: process_template_response (when the response has a render method), process_exception (on view errors). Caching commonly sits in this pipeline — serve common data from cache instead of hitting the view.


Relationships


References

  • Making queries — Django documentation
  • Django: Class Based Views vs Function Based View
  • Django 面试题 (博客园)