djangosnippets.org: Latest snippets tagged with 'middleware'https://djangosnippets.org/tags/middleware/2020-03-08T04:24:53.693829-05:00Django Language Middleware
2020-03-08T04:24:53.693829-05:00agusmakmunhttps://djangosnippets.org/snippets/10769/<p>This example below to setup default language code as <code>id</code> (Indonesian).</p>
Freely redistributableDjango Standard API Response Middleware for DRF
2019-12-11T08:02:03.303049-06:00agusmakmunhttps://djangosnippets.org/snippets/10717/<p>As you can see, if you using django-rest-framework, you will found many different response format. This middleware to solve all of these problems with Standard API Response.</p>
<p>All HTTP Response status stored into json response, not in HTTP Status (because mobile application, like android can't fetch the response body when …</p>
Freely redistributableGeoip middleware to restrict users to a set of allowed countries
2019-06-26T07:19:45.268959-05:00pakalhttps://djangosnippets.org/snippets/10675/<p>This middleware uses Django's Geoip support (https://docs.djangoproject.com/fr/2.2/ref/contrib/gis/geoip2/), as well as axes's package helper to retrieve IP address (since Django's REMOTE_ADDR might be wrong when behind a reverse proxy).</p>
<p>Ensure your geolite DB files are up to date (eg. with https://djangosnippets.org/snippets/10674/).</p>
<p>The checker is optional, but ensures that security is not …</p>
Freely redistributableDry basic-auth middleware using Django's AUTHENTICATION_BACKENDS and "basicauth" lib
2019-06-13T03:46:44.351320-05:00pakalhttps://djangosnippets.org/snippets/10671/<p>Requires to install "basicauth" package, which does basic-auth header encoding/decoding cleanly according to RFCs. </p>
<p>Could be improved to return a "realm" in case of http401, like in https://djangosnippets.org/snippets/1720/, although I'm not sure it's really useful in django usecases.</p>
Freely redistributableLoginRequiredMiddleware
2019-04-06T08:16:15.123452-05:00aasmprohttps://djangosnippets.org/snippets/10663/<h3>settings.py</h3>
<pre><code>MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'utils.LoginRequiredMiddleware',
]
LOGIN_REQUIRED_URLS = [
r'^panel/(.*)$'
]
</code></pre>
<p>this will help any url under <code>panel/</code> require login.</p>
Freely redistributableDjango Online Now Users - Middleware
2017-08-05T04:17:44.157367-05:00agusmakmunhttps://djangosnippets.org/snippets/10617/<p>django online users, usage: <code>{{ request.online_now }}</code> or <code>{{ request.online_now_ids }}</code>, complete tutorial: https://python.web.id/blog/django-count-online-users/, this snippet forked from: https://gist.github.com/dfalk/1472104</p>
Freely redistributableDjango 1.10 Internationalization Middleware
2016-11-16T22:32:54.999996-06:00agusmakmunhttps://djangosnippets.org/snippets/10594/<p>Django 1.10 Internationalization Middleware, more complete tutorial: https://python.web.id/blog/how-to-implement-django-internationalization/</p>
Freely redistributableJSON Web Token authentication middleware
2016-04-13T14:03:15.233277-05:00andruwharthttps://djangosnippets.org/snippets/10562/<p>This hasn't been thoroughly tested yet but so far it works great. We had no use for sessions or the built in authentication middleware for django as this was built to be a microservice for authentication. Unfortunately if you just use the django rest framework-jwt package the authentication occurs at …</p>
Freely redistributableMiddleware for fixing Microsoft Office (Word, Excel, Powerpoint) hyperlinks to views requiring authentication
2015-10-29T11:09:08.770388-05:00shapiromatronhttps://djangosnippets.org/snippets/10538/<p>Hyperlinks to views requiring authentication in Microsoft Office (Word, Excel, Powerpoint) can fail based on how Office handles rendering. This middleware sends a refresh return to the client, which will allow the page to be opened as normal, instead of the "Unable to open ... Cannot download the information you …</p>
Freely redistributableAlways have CSRF and Session cookies
2015-07-14T17:35:53.401368-05:00andrewhttps://djangosnippets.org/snippets/10512/<p>Middleware that ensures clients always have CSRF tokens and session ids. Useful for some fat-client apps.</p>
Freely redistributableLocaleMiddleware without browser language discovery
2015-02-26T14:20:52.602413-06:00ivellioshttps://djangosnippets.org/snippets/10469/<p>This snippet holds your Django project from automatically changing language of the page to the best fitting one by discovering the client browser language.</p>
<p>I personally needed to show the page to the user for the first time in the default language (English), although there were some translations. User can …</p>
Freely redistributableMiddleware decoratores for classed based views
2014-08-27T05:01:06.289396-05:00dhkehttps://djangosnippets.org/snippets/3091/<p>Decorators to attach middleware to class based views w/o arguments.</p>
Freely redistributableRestMiddleware - automatically load objects from url-path
2014-08-27T04:58:07.604108-05:00dhkehttps://djangosnippets.org/snippets/3090/<p>Automatically load models when their IDs appear in the URL-path.</p>
<p>See docstrings for usage and example.</p>
Freely redistributableSSL Force Middleware
2014-05-24T15:07:15.426150-05:00careercoderhttps://djangosnippets.org/snippets/3067/<p>A simple way to force SSL on all pages. It's very simple at this point - the only issue I can see right now and I will address this later is if someone sends http:// in another portion of your url.</p>
Freely redistributableExtended Profiling and Timing Middleware using cProfile
2013-10-27T17:34:57.073456-05:00dstrykehttps://djangosnippets.org/snippets/2998/<p>Modified version of <a href="http://djangosnippets.org/snippets/605/">Extended Profiling Middleware</a></p>
Freely redistributable