Business
Technology
- Administration
- Android
- Apache Solr
- API
- Development Principles
- Django
- Eclipse
- Hibernate
- Javascript/jQuery
- Microsoft .NET
- Microsoft Office
- Patterns
- PHP
- Social Media
- Spring 3.0
- Spring Roo
- Symfony
- Symfony: Doctrine
- Symfony: View
- Symfony: Web Services
A fundamental rule in technology says that whatever can be done will be done.
Andrew Grove
Django: Creating a Custom Middleware Component
To create your own custom django Middleware you need to do 4 things
- Define a module
- Ensure that the module is in the Python path
- Load the module in the INSTALLED_APPS section of the settings.py file
- Load the middleware in the MIDDLEWARE_CLASSES section of the settings.py file
- Add functionality
Define the module
If you dont know how to do this please read this article Django: Defining a Custom Module
Ensure that the module is in the python path
Everything from your "manage.py" file down will automatically be on your python path.
Load the module
Open your settings.py file and find the INSTALLED_APPS section
Add a new entry in the array like below
INSTALLED_APPS = (
'cms',
'custom',
)
Load the Middleware class
Open your settings.py file and find the MIDDLEWARE_CLASSES section
Add a new entry in the array like below
MIDDLEWARE_CLASSES = (
'custom.middleware.proxy.PageLikedProxy',
'django.middleware.common.CommonMiddleware',
)
- custom/middleware lives next to the manage.py file
- proxy is the proxy.py file
- and PageLikedProxy is the name of the class in the proxy.py file
One thing to remember is that the middleware classes are executed in the order that they appear in this array
Add functionality to your middleware
Open the proxy.py file and add the following code
class PageLikedProxy(object):
def process_request(self, request):
return None
For more information about django middleware modules please read the official documentation at Django Middleware
Add a comment

No comments yet. Please leave a comment using the form below