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

Forbidden (403) CSRF verification failed. Request aborted

I am making an app of login form but when I am running my app and click on login button the following error will occur Forbidden (403) CSRF verification failed. Request aborted. the code of view.py is as: from django.template import loader from…
user786
  • 383
  • 1
  • 3
  • 15
15
votes
2 answers

'WSGIRequest' object has no attribute 'session'

I get this error sometimes in custom Middleware in process_response method. I have the following list of middlewares: MIDDLEWARE_CLASSES =…
sunprophit
  • 1,639
  • 4
  • 16
  • 39
13
votes
1 answer

Django - application specific middleware

I am aware of the following methods for adding middlewares 1) Adding custom middleware component to django using MIDDLEWARE_CLASSES MIDDLEWARE_CLASSES = ( '......' 'path.to.custom.middlware',) 2) Adding view specific middleware using…
Wickkiey
  • 4,446
  • 2
  • 39
  • 46
11
votes
4 answers

Editing response content in Django middleware

I have Django 1.10 project and the following user-defined middleware class RequestLogMiddleWare(object): def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response =…
Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121
11
votes
3 answers

Where in django is the default 500 traceback rendered so that I can use it to create my own logs?

I would like to use it to generate html log files inside the process_exception() method of my custom middleware class, e.g: Exception caught. process_exception(request) called. process_exception calls whatever function returns default error…
10
votes
3 answers

Access named URL parameter in Template or Middleware

In my url conf, I have several URL's which have the same named parameter, user_id. Is it possible to access this parameter either in a middleware - so I can generically pass it on to the context_data - or in the template itself? Sample URL conf to…
Sjaak Trekhaak
  • 4,906
  • 30
  • 39
10
votes
1 answer

Django - Runtime database switching

In my work we want to run a server with multiple databases. The databases switching should occur when you acces a url like http://myapp.webpage.com or http://other.webpage.com. We want to run only one server instance and at the moment of the HTTP…
9
votes
2 answers

Django Error Reporting - How to know which user triggered the error?

Is there a way I can customize Django error reporting so when it emails me it lets me know which user triggered the error? I'm in Django 1.2 if it matters. Much Thanks in advance!
Greg
  • 45,306
  • 89
  • 231
  • 297
9
votes
1 answer

Calling a custom middleware after Authentication Middleware

In django REST framework authentication middleware sets the user object in request ONLY after the views middleware is executed while any custom middleware is executed before that. is there someway to change this order and execute custom middleware…
anubysh
  • 576
  • 6
  • 18
9
votes
2 answers

What's the sequence of middleware execution in django when error occurs in process_request?

I am looking into django middleware codebase. I looked into following diagram So, the diagram is quite clear. But I have some questions What happens when exception comes in process_request() middleware ? How is it handled ? Will the…
9
votes
4 answers

AttributeError: 'WSGIRequest' object has no attribute 'session'

I keep getting this error at random times and whenever I touch the django.wsgi file, it gets fixed only to happen again after a few hours. I'm lost as to what to do. my middleware_classes is as follows: MIDDLEWARE_CLASSES = ( …
9
votes
3 answers

Django ImportError: No module named middleware

I am using Django version 1.8 and python 2.7. I am getting the following error after running my project. Traceback (most recent call last): File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run self.result = application(self.environ,…
priya vyas
  • 195
  • 1
  • 1
  • 10
8
votes
2 answers

Django JWT authentication - user is anonymous in middleware

I am using Django JWT to power up authentication system in my project. Also, I have a middleware, and the problem is that inside it, the user is anonymous for some reason, while in the view I am able to access the correct user by request.user. This…
Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121
8
votes
2 answers

Django Response Middleware: Get name of view which created the response

I have simple middleware which checks if the HTML in the response is valid or not. If the HTML is not valid an html is not valid" exception gets raised in development systems. Up to now the xception contains the URL and the validation error. Then…
guettli
  • 25,042
  • 81
  • 346
  • 663
8
votes
4 answers

Accessing the current view class instance in Django middleware

Question: I'm trying to access an attribute of the view instance in the middleware layer. For example, given a class-based view like this: # views.py class MyView(View): my_attribute = 'something' I'd love to be able to get a handle on…
tino
  • 4,780
  • 5
  • 24
  • 30
1
2
3
30 31