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

Django: Cannot access variable set in middleware

I am trying to add a variable to my request variable in my custom middleware. Later I want to access that variable from my view. Here are the relevant bits: middleware.py class DomainInterceptMiddleware(object): def process_request(self,…
S B
  • 8,134
  • 10
  • 54
  • 108
2
votes
3 answers

How to raise 503 error in django?

I want to make site maintenance page and I create singleton model for site maintenance with checkbox field, So when I checked that checkbox from Django admin then the site should show maintenance page if we hit any URL of the website. I checked 503…
Neeraj Kumar
  • 3,851
  • 2
  • 19
  • 41
2
votes
1 answer

How to properly define a middleware class in Django v1.11?

I'm working on Django (v1.11) project and need to provide some middleware functionality to the site. This version of Django modify old MIDDLEWARE_CLASSES setting as described in docs: A new style of middleware was introduced for use with the new …
Yurii Rabeshko
  • 591
  • 8
  • 17
2
votes
1 answer

Understanding the process sequence in Django's middleware

I'm quite new to Django, and have been assigned the task of building a web application that authenticates a user against LDAP, once authneticated the user can search for a person/group in the LDAP Server. In the last few weeks I've been stuggling…
pymat
  • 1,090
  • 1
  • 23
  • 45
2
votes
0 answers

Django: Disable automatic language path prefix

I have a Django website with USE_I18N = True USE_L10N = True and added middleware: 'django.middleware.locale.LocaleMiddleware' Currently the website will add a language prefix (i.e. /en/) to my path in case the prefix is…
caliph
  • 1,389
  • 3
  • 27
  • 52
2
votes
1 answer

Excluding a Django app from being localized using a middleware

I need to localize a django project, but keep one of the applications (the blog) English only. I wrote this middleware in order to achieve this: from django.conf import settings from django.core.urlresolvers import resolve class…
Dor
  • 902
  • 4
  • 24
2
votes
1 answer

Django custom middleware to check header token.Tests with valid request header tokens still fails

I am checking a request header for a valid token in my custom middleware. class CustomTokenAuthentication(object): def process_request(self, request): access_token = request.META.get('HTTP_TOKEN', '') if…
2
votes
1 answer

Backport CSRF_TRUSTED_ORIGINS to Django 1.6

The Problem: In Django 1.9, CSRF_TRUSTED_ORIGINS was added to the available settings which allows to, for example, access the application from all the subdomains: CSRF_TRUSTED_ORIGINS = ["*.example.com"] Which is exactly what we need. The problem…
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2
votes
1 answer

Disable sessions/cookies for selected pages in Django

On my website, cookies and sessions are required only for authentication in the admin section. For all other urls I don't want to store cookies or run the session middleware, as it creates an unnecessary DB read/write on every http request. Is there…
BartoNaz
  • 2,743
  • 2
  • 26
  • 42
2
votes
0 answers

Django - using sqlalchemy with a read-only database

I'm using Django to nicely display stats etc from an Oracle database that I only have/want read access to. Because the database has composite primary keys I'm not using Django's ORM layer and I'm using SQLAlchemy instead. The only installed apps I…
pfctdayelise
  • 5,115
  • 3
  • 32
  • 52
2
votes
1 answer

Determine if response results from redirect

I have Django event logging application with middleware, which logs user page views. Currently, if response code is 200, log "User X visited page Y" is saved, but in case of redirect the log should be "User X has been redirected to page Y". Is it…
EMiDU
  • 654
  • 1
  • 7
  • 24
2
votes
2 answers

How do I do a Rails style before_filter with Google App Engine?

The app is setup as a basic WSGI application. I'm just trying to call a function before the requestHandler is run. I would like something very much like the way before_filter works in Rails. Thanks.
2
votes
1 answer

Django middleware process_template_response setup

I would like to create a function that checks if the user has any notifications. If they do, the number should be displayed in the navbar. Could someone please help me refactor this to do just that? Thank you! middleware.py: def…
jape
  • 2,861
  • 2
  • 26
  • 58
2
votes
1 answer

Django: How to get exception instance in error handlers

Django has built-in default views which are used automatically when exceptions such as PermissionDenied, Http404, SuspiciousOperation, etc. are raised. This is a convenient feature of Django which I love, but it seems to have a limitation. I am…
user193130
  • 8,009
  • 4
  • 36
  • 64
2
votes
1 answer

Is Django has something like "database middleware"?

Django has middleware to achieve do somthing when request and response. Is Django has "database middleware" to achieve do somthing when write data and read data. For example,some old database does not support to utf8mb4 , so it can't support all of…
cyhhao
  • 55
  • 6