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
8
votes
2 answers

Django: custom middleware called twice

I have a custom middleware which is called twice for every request and I don't understand why. This is my middleware: class MyMiddleWare(object): def process_request(self, request): print 'FOO' return None This is my middleware…
daveoncode
  • 18,900
  • 15
  • 104
  • 159
8
votes
1 answer

Django testing and middleware

I am having problems using the Django test Client() for testing middleware. It seems to emulate the sessions middleware specifically. However, since it is based on the RequestFactory, it does not seem to run any middleware. Is there any way to get…
Krystian Cybulski
  • 10,789
  • 12
  • 67
  • 98
7
votes
2 answers

Is there a way to alter the request.path before matching the urls?

When I get a request for a path that includes the word 'self' I want to replace it with the user id before matching it to a URL. I tried using a middleware like this: def process_request(self, request): if '/self/' in request.path: …
manuel
  • 837
  • 6
  • 18
7
votes
1 answer

How to access Django message framework content in Django unit tests

Using the Django messages framework, I am passing messages to the template to render in various scenarios - user account creation successful etc. The message is stored within the cookie for the session: print response.cookies['messages'] Set-Cookie:…
jvc26
  • 6,363
  • 6
  • 46
  • 75
7
votes
1 answer

Is changing SITE_ID dynamically in middleware considered good idea?

(this isn't duplicate of "Changing Django settings variable dynamically based on request for multiple site", as that previous question covers making much more serious reconfiguration at runtime) I use sites.Site to tie content to domains/host in my…
gorsky
  • 2,282
  • 1
  • 16
  • 22
7
votes
1 answer

Is it safe to use GZip Middleware in Django >= 1.10?

I am looking to enable text compression in Django. The performance docs reference GZip Middleware as the current solution for text compression. However, it comes with a stern warning: GZipMiddleware Compresses responses for all modern browsers,…
Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
7
votes
2 answers

Testing custom Django middleware without using Django itself

I have coded my custom Django middleware in the 1.10 style, similar to this: class MyMiddleware(object): def __init__(self, get_response): self.get_response = get_response # some initialization stuff here def __call__(self,…
Photon Light
  • 757
  • 14
  • 26
7
votes
2 answers

django error: ImproperlyConfigured: WSGI application

My application was working last night, not sure why it won't work this morning. I think that all I did was to create an app called django to store my models, tests, and views. Getting this error, running django with the Heroku Postgres application…
fox
  • 15,428
  • 20
  • 55
  • 85
7
votes
4 answers

Override Django authentication with middleware

I have a Django website and a MyBB forum, and I'd like to share authentication between them. My website used to be a message board; then I built a few other sections in Django, and both MyBB and Django run on the same domain. I've set up a system…
ySgPjx
  • 10,165
  • 7
  • 61
  • 78
7
votes
2 answers

Django middleware and HttpRequest change

I have a middleware to make some calculations/check for each incoming request. Some of view need this calculations result. As I do not want to call the same code twice, I would like to put results to HttpRequest in middleware, so view will be able…
Alan Harper
  • 793
  • 1
  • 10
  • 23
6
votes
2 answers

trigger function after returning HttpResponse from django view

I am developing a django webserver on which another machine (with a known IP) can upload a spreadsheet to my webserver. After the spreadsheet has been updated, I want to trigger some processing/validation/analysis on the spreadsheet (which can take…
dino
  • 3,093
  • 4
  • 31
  • 50
6
votes
1 answer

Is there any way to edit the request user in django middleware?

I am creating a way for Superusers to assume control of another user's account, but allow logging to show that all actions performed in this time are done by the superuser. The idea I have currently is to process the request in middleware and look…
Jamie J
  • 1,168
  • 1
  • 9
  • 22
6
votes
1 answer

how to define middleware only for certain paths in django?

is there anyway to define middleware for specific route or route groups in django? like laravel which we can define it as follows: Route::get('admin/profile', function () {})->middleware('auth');
unbl0ck3r
  • 371
  • 4
  • 16
6
votes
1 answer

Why doesn't Django's per-site cache middleware work for me?

I am using Django 1.3 beta 1 and set up memcached. I made changes to my settings.py per Django's instructions: CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', 'LOCATION':…
Belmin Fernandez
  • 8,307
  • 9
  • 51
  • 75
6
votes
2 answers

How do I reject a request prematurely with django?

I'm getting a lot of spambot requests (requests with referrer as a spam site). How do I reject a request prematurely with process_request on middleware so that django simply don't respond to the requests made from a specific referrer?
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
1 2
3
30 31