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

Tons of false positives from Django's CSRF middleware?

I'm getting tons of false positives from Django's contrib CSRF middleware. Just from normal use of the site there will be a lot of cases where the CSRF just starts blocking requests as suspected forgery attacks. Does anyone else have issues like…
MikeN
  • 45,039
  • 49
  • 151
  • 227
3
votes
3 answers

Modify address in Django middleware

I don't know if it's possible but I'd like to add few parameters at the end of the URL using middleware. Can it be done without redirect after modyfing requested URL? ie. user clicks: .../some_link and middleware rewrites it to:…
yezooz
  • 33
  • 4
3
votes
1 answer

Django: security middleware is crashing the site

On production, I've been trying to add the djangosecure.middleware.SecurityMiddleware (from http://pypi.python.org/pypi/django-secure)to my settings, but haven't had any luck making it work. When I run: ./manage.py checksecure Everything passes…
Konrad
  • 163
  • 2
  • 16
3
votes
2 answers

How can I prevent a redirect loop using SSL with gunicorn/nginx?

I get a redirect loop when using SSL middleware to redirect certain urls to HTTPS. What should I do? My nginx config is set up to forward requests to gunicorn.
fredley
  • 32,953
  • 42
  • 145
  • 236
3
votes
1 answer

Django Messaging Framework not displaying message despite RequestContext

here's a conundrum for you, Using Django 1.4, I cannot get messages set through the messages middleware to display in my templates. I have combed through the Django docs and ensured that my settings.py file referenced the relevant apps,…
3
votes
2 answers

Django: How to access session_key in middleware

I'm unable to access the session_key in my custom Django middleware. I try to access it using: session = Session.objects.get(pk=request.session._session_key) or session_key = request.COOKIES[settings.SESSION_COOKIE_NAME] session =…
Snels Nick
  • 925
  • 3
  • 13
  • 25
2
votes
3 answers

Django - Access Context Dictionary Before Template

I'm hoping to use a context processor or middleware to modify the values of the dictionary passed to render_to_response prior to the actual rendering. I have a messaging schema I'm trying to implement that would populate a message list based on the…
garromark
  • 814
  • 8
  • 20
2
votes
2 answers

Python changing modifying via imported function

I am trying to create a function, that when imported and then called, it will check and modify a tuple. I would like to be able to call this multiple times. However, I just have the function return the new variable, because I can not figure out a…
saul.shanabrook
  • 3,068
  • 3
  • 31
  • 49
2
votes
1 answer

Concatenate Django Settings "MIDDLEWARE_CLASSES"

I want to import the django default settings, using from django.conf import global_settings, and then add MIDDLEWARE_CLASSES to it. I like to do this with my settings so that I know what I have added and what is default. So my settings file would…
saul.shanabrook
  • 3,068
  • 3
  • 31
  • 49
2
votes
0 answers

Replacing Django auth app and middleware with custom auth app and authentication middleware

After researching on ways to extend user table of auth app in django , I have decided to write my own auth app. These are the things which I need to do for replacing the contrib.auth app: Create an AuthenticationMiddleware class and add an…
codecool
  • 5,886
  • 4
  • 29
  • 40
2
votes
1 answer

Function-Based Middleware vs Class-Based Middleware in Django

I read the doc, then I could understand that we can define a function-based middleware and a class-based middleware in Django as shown below but I could not understand the difference between them. *I'm learning Middleware: Function-Based…
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
2
votes
2 answers

How to implement Https(SSL Middleware) in Django

I am a newbie in Django and web development. I want to implement Exactly this Question, but in django. I have searched many blogs and questions, nowhere was i able to find,exactly how to implement this. The SSL Middleware Django, is something i…
Akamad007
  • 1,551
  • 3
  • 22
  • 38
2
votes
0 answers

How to write custom authentication backend for one endpoint only (/metrics) in Django?

I have a custom middleware in Django to force all the requests to go through a login authentication (with few exceptions like api/token). This project allows users to authenticate either via a JWT token or a login in /admin/login and all…
2
votes
1 answer

Custom authentication middleware for specific routes in Django

I implemented a custom authentication setup for my Django project. There are some user roles for users. Now I want to ensure that some specific routs may acceptable only of specific user roles. Let's say the edit/uploaded-files can be acceptable…
Amal S R
  • 870
  • 7
  • 21
2
votes
2 answers

How to get Django LoginRequiredMiddleware to return a 401_UNAUTHORIZED code instead of 302_FOUND

I am writing a custom basic LoginRequiredMiddleware for my Django project and so far everything works as expected. I am using process_request because I thought I wanted the request to be checked for authentication before the view is…
everspader
  • 1,272
  • 14
  • 44