Django: Creating a Custom Middleware Component

Django: Creating a Custom Middleware Component key visual

To create your own custom django Middleware you need to do 4 things

  1. Define a module
  2. Ensure that the module is in the Python path
  3. Load the module in the INSTALLED_APPS section of the settings.py file
  4. Load the middleware in the MIDDLEWARE_CLASSES section of the settings.py file
  5. 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

Owen commented:

Very nice post. I just stumbled upon your blog, Thank you for this nice article Thank you for taking time to share it with the readers, I am more than happy to have come across it thanks!and wanted to say that I have really enjoyed browsing your blog posts. cell phones for sale

Deoirt commented:

Everything is very, very clear, open is a description of the problem. It contains the information.<a href="http://www.besthairbase.com/full-lace-wigs">Full lace wigs</a>

Deoirt commented:

Everything is very, very clear, open is a description of the problem. It contains the information.<a href="http://www.besthairbase.com/full-lace-wigs">Full lace wigs</a>

lanimesalucha commented:

The author has written an excellent article. You made your point and not much to discuss. It's like this universal truth that you can not argue with the truth is not universal, everything has its exception. Thanks for this information. villas for rent thailand at http://www.asian-retreats.com Thanks :)

lanimesalucha commented:

The author has written an excellent article. You made your point and not much to discuss. It's like this universal truth that you can not argue with the truth is not universal, everything has its exception. Thanks for this information. villas for rent thailand at http://www.asian-retreats.com Thanks :)

lanimesalucha commented:

The author has written an excellent article. You made your point and not much to discuss. It's like this universal truth that you can not argue with the truth is not universal, everything has its exception. Thanks for this information. villas for rent thailand

ram14 commented:

Year 2013 has been quite eventful. SRSG also witnessed events and evolved in terms of business verticals, technologies, people and processes. Apple authorised service center Guwahati and Apple product dealers in Mumbai and Dejero 20/20 Transmitter and AKG headphones dealers and Octopus Newsroom Automation

aadi commented:

Guar protein offer a wide selection of products for your specific requirements.
Guar meal exporter india
Organic Poultry feed

active insurance advisors commented:

Insurance advisor confront peoples to become a life insurance sales person. One that has suffer the new agent status and now knows how to prospect for leads, makes big bucks, and cruises around in a big car. Life insurance advertising can be a sweet job. But did you know that the career agency is setting you up for stoppage.    <a href="http://www.activeinsuranceadvisors.com/">active insurance advisors</a>

active insurance advisors commented:

Insurance advisor confront peoples to become a life insurance sales person. One that has suffer the new agent status and now knows how to prospect for leads,    <a href="http://www.activeinsuranceadvisors.com/">active insurance advisors</a>

do partnership commented:

It is a common practice that when two or more people work together, they are generally more concerned with their ideas, innovations and creative directions, rather than their legal status. However, it is important to consider and resolve the rights, roles and responsibilities of each member of the partnership in writing before a misunderstanding or dispute arises. Written agreement helps in this regards. http://www.dopartnership.com/

Austin2z0 commented:

I am have been studying this topic for a long time. You have provided great information in you post and some things I have not seen in other content I have read by others.
best friend quotes

 

Tim Black commented:

I did not see the tarts made of onion. I hope it tastes good, different from common tarts. I like to visit recipe sites and i like yours too. All your products are very perfect and delicious. I have a recipe and I am searching a place to type my essay online. You will find it perfect. Thanks for the nice recipe, I will definitely try it.

 

fred commented:

 I'm impressed by the <a href="http://www.laanogsparpenge.dk/">details</a> that you have on this web site. It reveals how nicely you perceive this subject. Bookmarked this <a href="http://www.laan-info.dk/">webpage</a>, will come back for extra articles.

fred commented:

 I'm impressed by the details that you have on this web site. It reveals how nicely you perceive this subject. Bookmarked this <a href="http://www.laan-info.dk/">webpage</a>, will come back for extra articles.

Add a comment

*
*
Your email will not be published
*
*