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

Using middleware class for user authentication in Django project

One of my company's project is in Django and I was assigned to a task to 'add authentication to the top-level router' so we don't forget to add an authentication code to every view, such as: if not request.user.is_authenticated: return…
everspader
  • 1,272
  • 14
  • 44
2
votes
0 answers

Why we use first class function in Django custom middleware like this?

It's a custom middleware code: def my_middleware(get_response): print('One time initialization') def my_function(request): print('Its before view function') response = get_response(request) print('Its after view…
Hacnine
  • 21
  • 4
2
votes
1 answer

SystemCheckError in my Django website, why do these middleware errors appear?

So I was trying to run a web page in Django and I got these errors while django was checking everything: SystemCheckError: System check identified some issues: ERRORS: ?: (admin.E408) 'django.contrib.auth.middleware.AuthenticationMiddleware' must…
eneko valero
  • 457
  • 3
  • 14
2
votes
2 answers

Django allow only one user session in total

I currently try to implement a policy to my application that only one user session at a time is allowed, if a user trys to log-in from another device the old sessions gets killed. But for some resone i get the following error and i can't find the…
user10000033
2
votes
1 answer

Django Logging all the GET requests

I'm trying to put all the GET requests that appear in console into the database from django. For example: console log: [23/May/2019 13:58:44] "GET /testapp/ HTTP/1.1" 200 409 [23/May/2019 13:58:45] "GET /testapp/page2/ HTTP/1.1" 200 172 I'm trying…
Naeem Khan
  • 950
  • 4
  • 13
  • 34
2
votes
1 answer

Django, CSRF token makes an error! Where do I look at?

While I'm looking at log file, I got many error logs related to CSRF I got warning log below Forbidden (CSRF token missing or incorrect.): /my/site/uri and right after that error log below Internal Server Error: /my/site/uri Traceback (most recent…
Eric Lee
  • 700
  • 2
  • 9
  • 30
2
votes
0 answers

Caching per-app in Django rather than per-site or per-view

Lets say I have two apps in my Django website one for the API and one for the html pages, all urls starting with /api/ are handled by the API app. I have setup two caches with a specific setup for each like so: CACHES = { 'default': { …
2
votes
1 answer

How to add Django middleware with custom user settings as a Python package?

I'm attempting to create Django middleware that modifies the request object by adding additional data. I want to package this middleware so that it only requires the following steps for setup: Install via PIP Add route to MIDDLEWARE in…
jtimmins
  • 367
  • 3
  • 13
2
votes
1 answer

Custom Middleware in Django with Exclusions

I require to check authentication via token during execution of certain views, while some views can be accessed without authentication. So, how do i make a middleware and exclude some views from it. Any other idea to solve this is appreciated.
Sumit Badsara
  • 769
  • 9
  • 23
2
votes
1 answer

Error when trying to use LoginRequiredMiddleware in Django

settings.py """ Django settings for tutorial project. Generated by 'django-admin startproject' using Django 2.0.2. For more information on this file, see https://docs.djangoproject.com/en/2.0/topics/settings/ For the full list of settings and…
Jay Suthar
  • 199
  • 1
  • 4
  • 14
2
votes
0 answers

Django: how to have multiple sites with different default language

The situation: I have a instance of Django that runs multiple sites, each site can be configured by an admin user (it doesn't use Django admin but an admin app created by me). The problem: The problem is that each site should have a different…
Nazkter
  • 1,040
  • 1
  • 11
  • 31
2
votes
1 answer

Django Middleware keeps redirecting, visiting admin-site not possible

I followed a Django tutorial and wrote a Middleware class, to be precise its a "Login-Required"-Middleware so Users are just allowed to do some things on the website when they are logged in. The problem is that the middleware works as it should be…
stofuser91
  • 43
  • 3
2
votes
1 answer

How to test new Django 1.10+ MIDDLEWARE classes

I'm upgrading existing middleware to a new style 1.10+ Django middleware. Previously it was one similar to this: class ThreadLocalMiddleware(MiddlewareMixin): """ Simple middleware that adds the request object in thread local storage.""" …
2
votes
0 answers

Django data handling in custom middleware

In my django webapp, I have to pass user data param(udp) in a request and return the same in response without modifying it in view. Request { "mobile": "111111111", "udp":[ { "key": "keyName", …
user2858738
  • 520
  • 4
  • 15
2
votes
3 answers

installing snippets

How do I install snippets in django? (specifically this) I have the file /{project}/snippets/EnforceLoginMiddleware.py and I have tried any number of permutations inside MIDDLEWARE_CLASSES to load it as well as googling django snippets install to…
Robert Johnstone
  • 5,431
  • 12
  • 58
  • 88