Create your own Django middleware

** 2/28 Fixed forgetting to upload files to the repository. .. .. ** **

Purpose

Create and use your own middleware with Django.

environment

What is Django middleware?

Pre-processing and request processing in the view Request and response are processed as post-processing of view.

Implementation

middlewares
|_ __init__.py
|_ sample_middleware.py

sample_middleware.py


class SampleMiddleware(object):
    """
Sample middleware,
Before the request is processed by view and
Just display the string on the screen before the response is returned to the client
    """
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        self.process_request(request) #Preprocessing
        response = self.get_response(request) #View processing
        self.process_response(request, response) #Post-processing

        return response

    def process_request(self, request):
        print("Request processing")

    def process_response(self, request, response):
        print("Response processing")

The routing and view preparation are appropriate. Any pre-processing and post-processing methods can be added (Of course it doesn't have to be process_ ~~)

Setting method

settings.Excerpt from py


MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'middleware_sample.middlewares.sample_middleware.SampleMiddleware',
]

motion

When you access http://127.0.0.1:8000/test/ with a browser, The following contents are displayed.

Display of server-side terminal when accessing the browser


Request processing
Processing view for Sample
Response processing
[27/Feb/2017 14:11:02] "GET /test/ HTTP/1.1" 200 22

reference

I have posted the sample code on GitHub https://github.com/Fufuhu/DjangoMiddlewareSample

Recommended Posts

Create your own Django middleware
[Django] Create your own 403, 404, 500 error pages
Create your own exception
How to create your own Transform
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Create your own DNS server with Twisted
Create a model for your Django schedule
Create your own Composite Value with SQLAlchemy
Create your first app with Django startproject
Create a wheel of your own OpenCV module
Initialize your Django app
Create Django Todo list
Memo to create your own Box with Pepper's Python
Create your own Big Data in Python for validation
Create your own Random Dot Stereogram (RDS) in Python.
[Blender × Python] Create your own function & summary so far
Create an API with Django
Django beginners create simple apps 3
Django beginners create simple apps 1
Tuning your Django admin site
Create ToDo List [Python Django]
Create a homepage with django
Shell to create django project
Django beginners create simple apps 2
Create a Django login screen
Install Django on your Mac
Create and list Django models
Reinforcement learning 23 Create and use your own module with Colaboratory
Django beginners create simple apps 5
Create your own graph structure class and its drawing in python
Try docker: Create your own container image for your Python web app
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
[Python] Make your own LINE bot
Make your own manual. [Linux] [man]
[Python] logging in your own module
Register your Django application in your project
Solve your own maze with Q-learning
Create new application use python, django
Deploy your Django application on Heroku
Try implementing k-NN on your own
Until you self-host your own interpreter
Bridge ROS to your own protocol
Train UGATIT with your own dataset
Solve your own maze with DQN
Create a file uploader with Django
Create a LINE Bot in Django
Create your own virtual camera with Python + OpenCV and apply original effects
[Introduction to pytorch-lightning] How to use torchvision.transforms and how to freely create your own dataset ♬