Login

All snippets

Snippet List

Closest ORM models to a latitude/longitude point

Here's how to find the 10 closest locations to a point for a model that has latitude and longitude columns, if you're not using GeoDjango. This runs a brute force distance calculation against every row, so it should only be used on smaller tables - probably less than 100,000 rows. For larger tables you should use GeoDjango instead. See also [my TIL](https://til.simonwillison.net/postgresql/closest-locations-to-a-point) about this.

  • orm
  • gis
  • latitude
  • longitude
  • location
Read More

Django Collapsed Stacked Inlines

A simple jQuery javascript that collapses all stacked inline rows for better handling of large inline fieldsets. It also adds "Show"/"Hide"-buttons for showing/hiding each row, which could be customized and styled using css. **Usage (see below for example):** Include the javascript on your admin page, together with jQuery, and it'll automatically affect all stacked inlines. **Works with** Django 3.1.4 (Might work with other versions with or without adjustments, but not tested) **Use example:** *admin.py:* class DateInline(admin.StackedInline): model = Date extra = 10 class EventAdmin(admin.ModelAdmin): inlines = [DateInline] class Media: js = ['js/collapsed-stacked-inlines.js'] admin.site.register(Event, EventAdmin)

  • javascript
  • admin
  • jquery
  • inline
  • collapse
  • stacked
Read More

ReadOnlySelect

A minimally invasive Select widget that looks and acts like a disabled select input, but still submits with the form. It works by rendering the form as disabled, and supplements it's output with a hidden input field with the same name and value. Tested in Django 3.0, probably works in Django 1.11+

  • forms
  • select
  • readonly
Read More

Verify events sent to your webhook endpoints

Third party services (e.g. Stripe) optionally sign webhook events to verify it is them sending events. If the third party you use does not provide an SDK or official library for verifying signatures, you can manually verify the signature with this snippet.

  • signature
  • third-party
  • webhook
  • hmac
Read More

Read image url as base64

Read the image url as base64 in django, this snippet usefull if you using the `django-easy-pdf` to solve this issue https://github.com/nigma/django-easy-pdf/issues/53 Usage example: ``` <img src="{{ profile.photo.url|read_image_as_base64 }}"> ```

  • django
  • image
  • templatetags
  • base64
  • imagepreview
Read More

Django Standard API Response Middleware for DRF

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. All HTTP Response status stored into json response, not in HTTP Status (because mobile application, like android can't fetch the response body when HTTP Status >= 400).

  • middleware
  • django
  • api
  • django-rest-framework
Read More

3109 snippets posted so far.