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
5
votes
3 answers

Django 'WSGIRequest' object has no attribute 'set_cookie'

I keep on getting this exception when I do request.set_cookie() in the process_view of a custom middleware class. Here is the order of middleware classes in my settings.py: MIDDLEWARE_CLASSES = ( …
Dmitriy Smirnov
  • 449
  • 4
  • 15
5
votes
2 answers

'User' Object has no attribude is_authenticated

I've created a User model for my django app class User(Model): """ The Authentication model. This contains the user type. Both Customer and Business models refer back to this model. """ email = EmailField(unique=True) name =…
4
votes
2 answers

Django middleware: Isn't a middleware module error

I am using the middleware provided in https://gist.github.com/426829 to do cross site scripting. However, when I add the middleware to MIDDLEWARE_CLASSES, I get the error: ImproperlyConfigured: isn't a middleware module. My MIDDLEWARE_CLASSES…
sbidwai
  • 608
  • 9
  • 25
4
votes
1 answer

Pythonic/djangonic way to handle user timeouts on the scale of seconds (or minutes if need be)

The closest example to my case is a Django ajax chat application. The rooms need to keep a list of active users. Aside from displaying that list of users within the chat, a given room might have a maximum number of active users; new users need to be…
floer32
  • 2,190
  • 4
  • 29
  • 50
4
votes
2 answers

Django change request.path in middleware (to authenticate by token in url)

Dear omnoscient beings at Stackoverflow, In Django 1.3 I am making a process_request middleware that gets a token from an url, logs the user in (if it's correct) and removes the token from the url. However: I) Django recommends against accessing…
Mark
  • 18,730
  • 7
  • 107
  • 130
4
votes
1 answer

How costly is it to put a middleware in django

I have written for a very simple app of mine . To login facebook canvas users of the app To do some visitor tracking , most of the code is very simple. The app probably is not that complicated , fairly close to a blogging system . However I wish…
dusual
  • 2,097
  • 3
  • 19
  • 26
4
votes
2 answers

How do I apply Django middleware everywhere except for a single path?

I'm using Python 3.9 with Django 3. I have defined this middleware ... MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', …
Dave
  • 15,639
  • 133
  • 442
  • 830
4
votes
0 answers

Django: How do I allow views and methods with AllowAny permission to bypass authentication with HttpOnly JWT token cookies?

I have applied JWT authentication with HttpOnly cookies using a custom middleware. Now, the middleware checks for the presence of access and refresh tokens as HttpOnly cookies in every request. Everything works well. But, it retuns 'KeyError':…
4
votes
1 answer

Django Logger vs Logging Middleware

I want to log every request sent to Django. There are some posts online about logging middleware. The Django documentation talks about logger configuration and it seems that I can set it up to log everything without writing middleware. Can I log…
power
  • 1,680
  • 3
  • 18
  • 30
4
votes
3 answers

Django: creating/modifying the request object

I'm trying to build an URL-alias app which allows the user create aliases for existing url in his website. I'm trying to do this via middleware, where the request.META['PATH_INFO'] is checked against database records of aliases: try: src:…
Izz ad-Din Ruhulessin
  • 5,955
  • 7
  • 28
  • 28
4
votes
1 answer

Django 1.10: "new style" middleware equivalent of `process_request()`

How would one create "new style" middleware, which fulfills an equivalent implementation to using the process_request() hook with the "old style"? I've already adapted pre 1.10 middleware process_request() using MiddlewareMixin... from…
conner.xyz
  • 6,273
  • 8
  • 39
  • 65
4
votes
2 answers

Django: Error: ImproperlyConfigured Module does not define a " " class

ImproperlyConfigured: Middleware module "report" does not define a "ReportMiddleware" class But I had defined this, but probably defined in wrong place, so where should I place this class file?
user469652
  • 48,855
  • 59
  • 128
  • 165
4
votes
3 answers

How Do Search Engines See A Localized Django Site?

I have a Django site that uses the localization middleware in combination with gettext and the trans/blocktrans template tags to show visitors different pages depending on the preferred language in their user agent string (which seems to be the…
4
votes
2 answers

Django: How to set default language in admin on login

I'm saving an user's default language in his user profile and on login I want to set the admin's default language to it. One possibility I was thinking of is using a middleware, but I think if I do it on process_request I will not see an user…
Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
4
votes
6 answers

Django: Remove duplicate messages from storage

I'm using messages to add flash messages to the template (just as you'd expect). The problem I have is that if you double click a link to a page that generates a message then the message appears twice. I am using the message to tell the user I have…
rockingskier
  • 9,066
  • 3
  • 40
  • 49