Questions tagged [django-middleware]

Middleware is a framework of hooks into Django’s request/response processing. It’s a light, low-level “plugin” system for globally altering Django’s input and/or output.

Middleware is a framework of hooks into Django’s request/response processing. It’s a light, low-level “plugin” system for globally altering Django’s input and/or output. Each middleware component is responsible for doing some specific function.

Django ships with some built-in middleware you can use right out of the box; they’re documented in the built-in middleware reference.

455 questions
6
votes
1 answer

How to protect session against theft?

Simple test: On one machine I am logged to site (https) I entered to the same page on different machine (not logged in) I switched session_id in header on second machine - from first machine On second machine I get all of first machine - I am…
Nips
  • 13,162
  • 23
  • 65
  • 103
5
votes
1 answer

Django prints error when PermissionDenied exception raises

In our project, we have used django SessionMiddleware to handle users sessions and it's working fine. The only problem here is when PermissionDenied exceptions happens, an error and its traceback will be printed out in the console! However as…
Hamidreza
  • 1,465
  • 12
  • 31
5
votes
2 answers

For a Django middleware class, how can process_request work just fine, but process_exception not be called?

I've created my own middleware class in Django that was working just fine until recently. The strange thing is that process_request still gets called just fine, but even when the response is 500 - internal server error, process_exception is not…
Trindaz
  • 17,029
  • 21
  • 82
  • 111
5
votes
2 answers

Database Routing by URL parameter

I'm developing on a Django Project with several Databases. In an App I need to switch the database connectivity from a Development-Database to Test-DB or Production-DB, based on a request from the user. (DB Architecture is set and not changeable!) I…
5
votes
1 answer

is there any way to modify the current logging to json format with few other fields added python logging

I have many application code written in python django and every application is using standard python logger module and just a simple message strings are getting logged. Now is there any way where I can add my own customer middleware or django app…
5
votes
1 answer

Django - How to modify template context from middleware

I am creating a Django middleware that manages a 'shopping cart' with sessions. I was able to successfully modify the session data like so: class ShoppingCartMiddleware: def __init__(self, get_response): self.get_response =…
Grayson Pike
  • 397
  • 1
  • 4
  • 13
5
votes
1 answer

Django Logger: How to log Username

I am trying to implement logging in my Django project (django 1.11, Python 3.6). I'm using default django logger. To get the username in log, I have used django-requestlogging 1.0.1. As of now, I don't have any user other than admin superuser. When…
5
votes
1 answer

Setting cookies within Django Middleware

I would like to use Custom Django Middleware (Django 1.9) to check whether or not an anonymous user has accepted a site's terms & conditions - I'll show the user a dialog if they've not yet clicked 'Agree'. I have no need to store this in the…
Paul J
  • 799
  • 1
  • 6
  • 16
5
votes
2 answers

Testing django pages with middleware for multihost

Background: I'm using the middleware django-multihost (http://effbot.org/zone/django-multihost.htm) to allow my single django app to respond to different hostnames from the same project. The middleware changes the ROOT_URLCONF (i.e. which urls.py…
Leopd
  • 41,333
  • 31
  • 129
  • 167
5
votes
1 answer

Django 1.10 and Middleware

Once more: Django 1.10. New middleware style. In the documentation we…
Trts
  • 985
  • 1
  • 11
  • 24
5
votes
1 answer

Safe to modify settings.SITE_ID from middleware in Django?

I have modified the multihost.py middleware I found at http://effbot.org/zone/django-multihost.htm to set the settings.SITE_ID dynamically, but have some concerns that I may have just left the reservation. Most examples I have found for multiple…
Ryan Townshend
  • 2,404
  • 21
  • 36
5
votes
1 answer

Autologout a user after specific time in django

I need to logout a user after some specific time(lets take it as 1 min for now), and so created a middleware class as below myproject/middleware.py from datetime import datetime, timedelta from django.contrib import auth class AutoLogout: def…
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
5
votes
2 answers

Django: Overwrite ROOT_URLCONF with request.urlconf in middleware

I am trying to overwrite ROOT_URLCONF with another url when the request contains "api" subdomain and this is what I have so far. from django.utils.cache import patch_vary_headers class SubdomainMiddleware: def process_request(self, request): …
5
votes
3 answers

'WSGIRequest' object has no attribute 'user'

I'am trying to make an auth module in my django project. But when I open my web site url I have a this error: 'WSGIRequest' object has no attribute 'user' I've trying to find information about this problem and somebody said that the problem is in…
Anton Erjomin
  • 183
  • 1
  • 1
  • 9
5
votes
3 answers

Django Middleware: How do I access a view's params from middleware

Let's say I have a view: def pony_view(request, arg1, arg2): ... Make ponies, etc ... And a middleware: class MyMiddleware(object): def process_request(request): # How do I access arg1, arg2 here? Of course arg1, and arg2 will be…
orokusaki
  • 55,146
  • 59
  • 179
  • 257