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
3
votes
1 answer

ImportError: Module "whitenoise.middleware" does not define a "WhiteNoiseMiddleWare" attribute/class

In my django app white noise is acted on in the following ways: MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleWare', and STATICFILES_STORAGE =…
user9487981
3
votes
0 answers

Django Middleware and threading to catch the current user

I'm trying to use catch the Django user in the Middleware but without success. Using Python 3.6 and Django 1.11. from threading import local _user = local() class CurrentUserMiddleware: def __init__(self, get_response): …
André
  • 24,706
  • 43
  • 121
  • 178
3
votes
1 answer

How to implement the SimpleMiddleware?

I am struggling getting MiddleWare to work. I put this in my settings.py: MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', …
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
3
votes
1 answer

Tracking unique users hitting certain urls in Django project

In a Django project of mine, I want to log all unique user IDs visiting a certain section of the web application. Currently, the only distinguishing feature of this section is that it's url patterns are written in a separate module. What would be…
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
3
votes
1 answer

How to use multiple flatpages models in a django app?

I have multiple models that can be converted to flatpages but have to have some extra information (For example I have an about us page but I also have a blog). However I understand that there must be only one flatpages model since the middleware…
the_drow
  • 18,571
  • 25
  • 126
  • 193
3
votes
1 answer

Best practice to serve static files in Django

What is the best practice to serve image files in Django for production? I want to respond with static images and I'm deploying my Django app to Heroku. Are there any significant drawbacks of using django.middleware.security.SecurityMiddleware…
Manan Mehta
  • 5,501
  • 1
  • 18
  • 18
3
votes
1 answer

Django 1.10: login required middleware redirect loop

I'm writing a bit of middleware to effectively make @login_required on all pages. Unfortunately what I've got results in a redirect loop. The implementation is using "old" style middleware with 1.10 via MiddlewareMixin and process_request() hook in…
conner.xyz
  • 6,273
  • 8
  • 39
  • 65
3
votes
3 answers

How to adding middleware to Appengine's webapp framework?

I'm using the appengine webapp framework (link). Is it possible to add Django middleware? I can't find any examples. I'm currently trying to get the FirePython middleware to work (link).
Sam
  • 6,240
  • 4
  • 42
  • 53
3
votes
2 answers

Django multiple database mapping

In Django when using multiple DB's how to map databases, for ex: from request there is a parameter as app1 which maps to db1 and another request app2 mapping to db2. Now how to choose db's much before sending the request to views…
Rajeev
  • 44,985
  • 76
  • 186
  • 285
3
votes
2 answers

Django 1.8: ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class

I started my virtualenv. Then started my server via python manage.py runserver. I ended up visiting my django site and getting the following error. (venv)N$ python manage.py runserver Validating models... 0 errors found June 11, 2015 -…
ApathyBear
  • 9,057
  • 14
  • 56
  • 90
3
votes
0 answers

Django: Disable cache_page in middleware or conditionally on a request parameter

Suppose I have a middleware that detects whether a particular request comes from a particular machine: class HomeIpMiddleware: def process_request(self, request): request.home_ip = False ip = request.META.get("REMOTE_ADDR") …
awidgery
  • 1,896
  • 1
  • 22
  • 36
3
votes
0 answers

Prevent cache middleware from using HTTP_COOKIE to generate cache key

I'm having problems issues using the Django caching middleware. I want to cache an expensive page so that it does not need to be regenerated for each individual visitor. It seems that SessionMiddleware is setting "Vary: Cookie" in the response…
aco
  • 719
  • 2
  • 9
  • 26
3
votes
1 answer

how to pass a value to every view with django middleware

I have a middleware function that overrides process view. I want to pass a variable to every view. Is the best place to do this in the request, args or kwargs parameter to view_func? I tried this with no luck: def process_view(self, request,…
Atma
  • 29,141
  • 56
  • 198
  • 299
3
votes
4 answers

Detect the language & django locale-url

I want to deploy a website in English & Spanish and detect the user browser language & redirect to the correct locale site. My site is www.elmalabarista.com I install django-localeurl, but I discover that the language is not correctly detected. This…
mamcx
  • 15,916
  • 26
  • 101
  • 189
3
votes
1 answer

Django - AuthenticationMiddleware setting request.user

Regarding AuthenticationMiddleware, why did Django 1.3 handle the user property at class level, and why was it changed to an instance property in 1.4? This is from 1.3 class LazyUser(object): def __get__(self, request, obj_type=None): …
user1563285