Wednesday, September 12, 2012

write django middleware


write django middleware - Google Search

https://docs.djangoproject.com/en/dev/topics/http/middleware/


Writing your own middleware

Writing your own middleware is easy. Each middleware component is a single Python class that defines one or more of the following methods:

process_request

process_request(selfrequest)
request is an HttpRequest object. This method is called on each request, before Django decides which view to execute.
process_request() should return either None or an HttpResponse object. If it returns None, Django will continue processing this request, executing any other middleware and, then, the appropriate view. If it returns an HttpResponse object, Django won't bother calling ANY other request, view or exception middleware, or the appropriate view; it'll return that HttpResponse. Response middleware is always called on every response.

process_view

process_view(selfrequestview_funcview_argsview_kwargs)
request is an HttpRequest object. view_func is the Python function that Django is about to use. (It's the actual function object, not the name of the function as a string.) view_args is a list of positional arguments that will be passed to the view, and view_kwargs is a dictionary of keyword arguments that will be passed to the view. Neither view_args nor view_kwargsinclude the first view argument (request).
process_view() is called just before Django calls the view. It should return either None or an HttpResponse object. If it returnsNone, Django will continue processing this request, executing any other process_view() middleware and, then, the appropriate view.



https://docs.djangoproject.com/en/dev/ref/middleware/


Middleware

This document explains all middleware components that come with Django. For information on how to use them and how to write your own middleware, see the middleware usage guide.

Available middleware

Cache middleware

class UpdateCacheMiddleware
class FetchFromCacheMiddleware
Enable the site-wide cache. If these are enabled, each Django-powered page will be cached for as long as theCACHE_MIDDLEWARE_SECONDS setting defines. See the cache documentation.

“Common” middleware

class CommonMiddleware
Adds a few conveniences for perfectionists:





http://www.djangobook.com/en/beta/chapter16/

You're reading an outdated version of this book; a newer version is available.

Chapter 16: Middleware





No comments:

Post a Comment