Login

Most bookmarked snippets

Snippet List

Geoip middleware to restrict users to a set of allowed countries

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). Ensure your geolite DB files are up to date (eg. with https://djangosnippets.org/snippets/10674/). The checker is optional, but ensures that security is not broken due to a misspelled/missing GEOIP_COUNTRY_WHITELIST.

  • middleware
  • ip
  • country
  • geoip
  • restriction
  • blocker
Read More

updated django-mptt enabled FilteredSelectMultiple m2m widget, Django 1.11 compatible

Small fix to make https://www.djangosnippets.org/snippets/1779/ compatible with Django 1.11 To use this you'll also need the javascript from https://www.djangosnippets.org/snippets/1780/ I added an overridden optgroups method that can handle the additional tuple and a couple minor things I gathered while investigating for a fix (a default level_indicator of "+--" and changed paths for the JS files. All credits goes to @anentropic :-)

  • widget
  • mptt
Read More

StringField: CharField with no max_length for Postgres

Django's CharField requires a max_length, and TextField displays a multi- line widget, but in Postgres there's no reason to add an arbitrary max thanks to the `varlena` storage format. So this is a TextField that displays as a single line instead of a multiline TextArea.

  • orm
  • postgres
  • charfield
Read More

Django nginx sendfile example

Use nginx sendfile (X-Accel-Redirect) function to serve files but pass traffic through django. Can be used to serve media files only to logged-in users.

  • django
  • media
  • sendfile
  • nginx
Read More

LoginRequiredMiddleware

### settings.py 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/(.*)$' ] this will help any url under `panel/` require login.

  • middleware
  • authentication
  • login_required
Read More

spaceless_json

Now you can format and compress json-data in django template

  • django
  • templatetag
  • json
  • spaceless
  • formatting
  • application/id+json
Read More

Tweet embed template tag

Takes a tweet url, requests the json from Twitter oEmbed, parses the json for the html element and returns it to your template. The html returned is ready to go and will be shown as a tweet on your web page. This uses the Requests library for Python. A full example can be found on GitHub https://github.com/z3ke1r/django-tweet-embed.

  • django
  • templatetag
  • json
  • embed
  • twitter
  • parse
  • requests
  • tweet
Read More

Hand over model instance relations to another instance of the same model

If you have a model instance you want to merge into another, it's handy to hand over all the relations into the model you want to merge into, so the deletion won't trigger cascading deletions from other tables. You can pass an `Iterable` of the same objects (a.k.a `QuerySet`) to the model, and then process it with the new model's `pk`.

  • foreignkey
  • many2many
  • relations
  • handover
Read More

Auth decorators with 403 - Django 1.11

This decorator is based on user_passes_test, but when a user is logged in and fails the test, it will render a 403 error instead of redirecting to login - only anonymous users will be asked to login. Version working with Django 1.11, based on version found on [https://djangosnippets.org/snippets/254/] , which is deprecated.

  • auth
  • user_passes_test
  • 403 permission_required
Read More

3110 snippets posted so far.