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
113
votes
6 answers

How to set up custom middleware in Django?

I'm trying to create a middleware to optionally pass a kwarg to every view that meets a condition. The problem is that I cannot find an example of how to set up the middleware. I have seen classes that override the method I want to,…
Atma
  • 29,141
  • 56
  • 198
  • 299
81
votes
2 answers

Django exception middleware: TypeError: object() takes no parameters

I'm using Django 1.10 and trying to catch all exceptions with exception middleware. The code below causes an internal server error: mw_instance = middleware(handler) TypeError: object() takes no parameters views.py from django.http import…
user984003
  • 28,050
  • 64
  • 189
  • 285
55
votes
6 answers

Where to put Django startup code?

I'd like to have these lines of code executed on server startup (both development and production): from django.core import management management.call_command('syncdb', interactive=False) Putting it in settings.py doesn't work, as it requires the…
Attila O.
  • 15,659
  • 11
  • 54
  • 84
51
votes
9 answers

Non-global middleware in Django

In Django there is a settings file that defines the middleware to be run on each request. This middleware setting is global. Is there a way to specify a set of middleware on a per-view basis? I want to have specific urls use a set of middleware…
hekevintran
  • 22,822
  • 32
  • 111
  • 180
41
votes
5 answers

Execute code in Django after response has been sent to the client

In my Django application I want to keep track of whether a response has been sent to the client successfully. I am well aware that there is no "watertight" way in a connectionless protocol like HTTP to ensure the client has received (and displayed)…
Florian Ledermann
  • 3,187
  • 1
  • 29
  • 33
41
votes
9 answers

Change Django Templates Based on User-Agent

I've made a Django site, but I've drank the Koolaid and I want to make an IPhone version. After putting much thought into I've come up with two options: Make a whole other site, like i.xxxx.com. Tie it into the same database using Django's sites…
37
votes
13 answers

Python and Django OperationalError (2006, 'MySQL server has gone away')

Original: I have recently started getting MySQL OperationalErrors from some of my old code and cannot seem to trace back the problem. Since it was working before, I thought it may have been a software update that broke something. I am using python…
Franz Payer
  • 4,069
  • 15
  • 53
  • 77
36
votes
3 answers

How to return an rest_framework.response object from a django custom middleware class?

I am trying to write a middleware class that ensures that the user is logged in. But the problem is this middleware class will only be applicable to a small set of views and these views return a DRF's Response object rather then the HTTPResponse…
Mahammad Adil Azeem
  • 9,112
  • 13
  • 57
  • 84
32
votes
2 answers

Practical rules for Django MiddleWare ordering?

The official documentation is a bit messy: 'before' & 'after' are used for ordering MiddleWare in a tuple, but in some places 'before'&'after' refers to request-response phases. Also, 'should be first/last' are mixed and it's not clear which one to…
kolypto
  • 31,774
  • 17
  • 105
  • 99
30
votes
5 answers

Django: "No module named context_processors" error after reboot

I have a Django site that works on my PC, and was working briefly on my server after loading it on. I noticed my server had Django 1.6 and I upgraded to 1.8. After rebooting, none of the pages on my site load and I get the error: ImportError No…
Chris May
  • 582
  • 1
  • 4
  • 10
26
votes
3 answers

Context processors vs middleware in django

It seems to me that everything a context processor can do, middleware can do. So what's the point of context processors? Are they just middleware-lite?
pseudosudo
  • 6,270
  • 9
  • 40
  • 53
23
votes
3 answers

Conditional Django Middleware (or how to exclude the Admin System)

I want to use some middleware I wrote across the whole of my site (large # of pages, so I chose not to use decorators as I wanted to use the code for all pages). Only issue is that I don't want to use the middleware for the admin code, and it seems…
user127032
  • 231
  • 1
  • 2
  • 3
19
votes
6 answers

Django: WSGIRequest' object has no attribute 'user' on some pages?

I want to set a cookie if user is logged in or not. My middleware: class UserStatus(object): def process_response(self,request,response): user_status = 1 if request.user.is_authenticated() else 0 max_age = (20)*52*7*24*60*60 # 20…
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
18
votes
4 answers

'WSGIRequest' object has no attribute 'session' while upgrading from django 1.3 to 1.9

Similar to this question 'WSGIRequest' object has no attribute 'session' But my MIDDLEWARE classes are in the correct order. INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.admin', 'django.contrib.auth', …
Shawn Anderson
  • 323
  • 1
  • 2
  • 8
16
votes
4 answers

Disable a specific Django middleware during tests

How can I disable a specific middleware (a custom middleware I wrote) only during tests?
daveoncode
  • 18,900
  • 15
  • 104
  • 159
1
2 3
30 31