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

How do I change a language of a particular page in Django website

Django internationalization allows me to set a language code either in settings file (site-wide) or on per-user / per-session basis. How can I change language for a particular page? I wrote a middleware that sets request.LANGUAGE_CODE the way I want…
Yury Lifshits
  • 309
  • 1
  • 6
  • 13
4
votes
3 answers

Django middleware to determine user's group in a session

I have an app that uses django.contrib.auth but makes no use of Django's built-in permissions system. Instead, views have the @login_required decorator and then check which group the user belongs to, and follow different branches of code execution…
gbsmith
  • 113
  • 3
  • 9
4
votes
2 answers

Code to run in all views in a views.py file

What would be the best way of putting a bit of code to run for all views in a views.py file? I come from a PHP background and I normally put this in the constructor/index bit so that it always ran whatever page is being requested. It has to be…
user2355278
  • 375
  • 2
  • 5
  • 15
4
votes
2 answers

Use middleware or a custom template tag for infrequently changing snippet

I've got a small snippet that I want in my sidebar. The snippet will be visible on each page, and while cheap to fetch (about 50ms on my super-slow netbook!), will change so infrequently that I'd quite like to cache it (partly because I've yet to…
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
3
votes
2 answers

Django messages framework not working in template loop

I recently upgraded to Django 1.3 and I want to start using the Messages system. I have added my Middleware, Template context processors and also messages into the INSTALLED_APPS MIDDLEWARE_CLASSES = ( …
ApPeL
  • 4,801
  • 9
  • 47
  • 84
3
votes
1 answer

ContentNotRenderedError: The response content must be rendered before it can be accessed (Django Middleware)

I am creating Django middleware for blocking a user when (s)he gets throttled more than 5 times but I am getting ContentNotRenderedError. Full error msg: Traceback (most recent call last): File…
rain
  • 525
  • 6
  • 15
3
votes
0 answers

Which pattern is implemented in Django Middleware? (Chain of responsibility or Decorator)

I'm trying to figure out what pattern is used in Django Middleware. Maybe there is combination of patterns?
3
votes
1 answer

How to unittest "new style" Django middleware

I am learning how to unittest Django middleware. In the 'old style' Middleware, it was easy enough to load middleware using process_request() to test results. E.g. def test_session(self): request = self.factory.get('/') session_middleware =…
alias51
  • 8,178
  • 22
  • 94
  • 166
3
votes
1 answer

Django custom middleware request.user always AnonymousUser

I'm using Django 2.2 and DRF In my application, based on the user property, I need to change the request.user to a different user. class CustomMiddleware: def __init__(self, get_response): self.get_response = get_response def…
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
3
votes
1 answer

Django show message in every view

I want to show a message in every view in my Django project. Once a user has created an account, I send them an email asking them to verify their email and until they have verified their email address (by visiting a verify URL) I'll show the…
KerrM
  • 5,139
  • 3
  • 35
  • 60
3
votes
1 answer

Transaction beginning in Django

I read the chapter in the Django docs about the transaction management. From what I understand, the transaction is started as soon as TransactionMiddleware is invoked. Then by using @commit_on_success, @commit_manually there is a possibility of…
julx
  • 8,694
  • 6
  • 47
  • 86
3
votes
2 answers

Django rest framework custom Response middleware

I use Django Rest Framework with rest_auth (/login, /logout/, /register...) I have a Middleware, normally triggered by user_logged_in or user_logged_out signals. In my Middleware, I want to use Response object from Rest framework. My…
Isador
  • 595
  • 3
  • 10
  • 23
3
votes
1 answer

Pass extra arguments from middleware to view

I want to add another field in my POST(JSON) request. The key-value pair has to be "request id". I have concluded that the best way would be to generate a random request id and inject it into the request object using middleware. I have written a…
Tahseen Rahman
  • 97
  • 3
  • 13
3
votes
1 answer

Django 2.0 custom middleware process_exception hook not firing

My custom middleware class seems to be skipping the process_exception stage during the response cycle from django.utils.deprecation import MiddlewareMixin class MyMiddleware(MiddlewareMixin): def process_request(self, request): …
ptr
  • 3,292
  • 2
  • 24
  • 48
3
votes
2 answers

How can I add field to request body in django middleware

I want to add new field in request body in middleware and use it in views. I googled it but the results was not worked. How can I do it? Django v2 python 3.6
Mahbod_SN
  • 51
  • 1
  • 5