2023-06

Django June 2023: Tips & Discussions #DjangoJune by Trey Hunner

Django is definitely not the only thing I use Python for, but I use and teach Django quite often. In the first half of 2023, I held 3 Django-related trainings, did some Django consulting, and I worked on the Python Morsels web app, which is written in Django.

My Django students and clients often ask questions that make me realize I have Django opinion that might be of interest to someone else (a fact that’s challenging to recognize in isolation)! So for the month of June I decided to post daily-ish social media threads (to both Mastodon and Twitter) on interesting Django-related ideas using the tag #DjangoJune.

Here’s a summary of the couple dozen Django tips from this #DjangoJune adventure, including links to the Mastodon threads and occasional links to the Twitter threads (when discussion on Twitter was sufficiently different).

2023-06-28 QuerySets are Lazy by Kátia Nakamura

Did you know that QuerySets are lazy?

QuerySets have a hidden gem 💎 called lazy evaluation. It is a powerful feature that allows you to postpone the execution of a database’s query until it’s absolutely necessary.

Let’s dive right in.

2023-06-23 Weekly Django news 185 , articles, projects, and more. Curated by Jeff Triplett & William Vincent

Updates to Django

From Django Review and Triage Team Member Sarah Boyce…

Last week we had 13 pull requests merged into Django by 10 different contributors - including 2 first time contributors!

Congratulations to Olivier Le Thanh Duong and Ashwin Dharne for having their first commits merged into Django - welcome on board!

This time filtering support was added to GIS aggregate functions, and an offset value was added to StepValueValidator! You will be able to use both of these features in 5.0.

Do you want to add something into Django 5.0? Why don’t you update assertContains and assertInHTML to output a haystack on failure #34657? Look forward to welcoming you on board!

Building Search DSLs with Django

What happens when there are too many fields for a UI to search on?

Search DSLs can give a user more granular access to searching without exposing an overly complicated interface.

Bringing Locality of Behavior to Django June 23rd 2023

An attempt via django-view-decorator to bring more locality of behaviour between Django URLs and views. Picking up on the concept first popularized by HTMX and highlighted by Carlton Gibson in his recent DjangoCon Europe talk, among other places.

Django Model Fields With Attributes

Jacob shows us how to use contribute_to_class, a Descriptor, and a string class to create a custom MarkdownField that can display both text and html.

Caching in Django with Redis

A step-by-step guide on implementing caching with Redis in Django.

Videos DjangoCon Europe 2023: Use SQLite in production

Tom Dyson revisits the traditional wisdom that SQLite is not suitable for production.

He takes a deep look at several real-world examples, comparing its performance against traditional database options (PostgreSQL, MySQL, etc).

And he explores exciting new developments in the SQLite ecosystem, particularly those which enable its use in machine learning in general and LLMs (large language models) in particular.

2023-06-21 Faire du gRPC simplement avec Django #Python #AFPy #Django #gRPC #Lyon

gRPC ( https://grpc.io/ ) est le nouveau protocole de communication haute performance basé sur HTTP2 🚀.

Il permet de prendre en charge nativement le streaming, le transport binaire, l’équilibrage de charge, la vérification de l’état de santé, la traçabilité et l’authentification.

C’est un outil parfait pour les API et les architectures de microservices.

Cependant, le prendre en main peut être un peu fastidieux et changer ses habitudes de développement peut être risqué pour les équipes de développement.

Nous proposons donc de découvrir la bibliothèque Django-socio-grpc , ( https://github.com/socotecio/django-socio-grpc ) qui suit la même logique que Django-REST-framework, mais avec gRPC.

Après cette présentation par Adrien et Léni, un moment d’échange a lieu.

Comme toujours, comportez vous en accord avec la charte de l’AFPy ! ( https://www.afpy.org/docs/charte , https://www.afpy.org/docs/a-propos )

Django-socio-grpc

2023-06-21 It seems that “The Locality of Behaviour principle” (shortened as LoB) is gaining traction these days

#Django #LoB #LocalityOfBehavior #HTMX

It seems that “The Locality of Behaviour principle” (shortened as LoB) is gaining traction these days.

This has given me the urge to try to influence the direction of Django to bring more LoB to the connection between views and URLs.

Discussion: https://forum.djangoproject.com/t/bringing-locality-of-behaviour-to-django-views-and-urls/21765

django-view-decorator

django-view-decorator is decorator aimed at bringing locality of behaviour to the connection between a URL and a view in Django.

2023-06-20 Découverte de djLint (HTML Template Linter and Formatter. Django • Jinja • Nunjucks • Twig • Handlebars • Mustache • GoLang • Angular )

Once upon a time all the other programming languages had a formatter and linter.

Css, javascript, python, the c suite, typescript, ruby, php, go, swift, and you know the others.

The cool kids on the block.

HTML templates were left out there on their own, in the cold, unformatted and unlinted :( The dirty corner in your repository. Something had to change.

Grab it with pip:

pip install djlint

Lint your project

djlint . --extension=html.j2 --lint

Check your format

djlint . --extension=html.j2 --check

Fix my format!

djlint . --extension=html.j2 --reformat

2023-06-17 Building Search DSLs with Django (and pyparsing)

Search capabilities span from free text (think Google) to raw data access (think SQL).

In between, there’s a wide range of options for narrowing a search that are often provided with UI elements. But what if there are too many fields for a UI to search on? Search DSLs can give a user more granular access to searching without exposing an overly complicated interface.

GitHub issues provide a DSL that’s accompanied by UI elements. An example query for searching issues would be:

is:open author:danlamanna

We can create something similar for use in a custom Django application. Taking this example means building a DSL that:

  • Supports filtering by a status of either open or closed

  • Supports filtering by an author username

  • Implicitly conjoins the status and author filter (AND them together)

Enter PyParsing

PyParsing is a fantastic open source library for creating parsers.

We can use it to build the “open or closed” part of this by creating a pyparsing.ParserElement.

import pyparsing as pp

pp.one_of('open closed')  # ParserElement

pp.one_of('open closed').parse_string('foo')
# ParseException: Expected open | closed, found 'foo'  (at char 0), (line:1, col:1)

pp.one_of('open closed').parse_string('open')
# ParseResults(['open'], {})

# pyparsing ignores whitespace by default
pp.one_of('open closed').parse_string(' open   ')
# ParseResults(['open'], {})

This lets us take an input string and return a set of ParseResults that we can build database queries from!

2023-06-19 Weeknotes 2023-06-19

2023-06-10 Addding template fragments or partials for the DTL

I’d like to begin a discussion about adding some kind of inline partials or template fragments feature to the DTL.

For background, there’s an HTMX essay on this topic here: Htmx ~ Template Fragments

The basic idea is that you can re-use parts of a template, when just updating part of a page say .

Currently the DTL requires moving the reusable fragment into an include or a template tag, which is great in many cases, but can be a little heavyweight (certainly to begin).

Being able to define these partials inline gives better Locality of Behaviour (a concept for which there’s another essay on the HTMX ).

There’s lots of history in the area — here’s one thread, that links to another, but there are many more similar ideas floated around — so there’s clearly some demand.

I gave a talk on this topic at DjangoCon Europe this year , and have put up a package called django-template-partials to help demonstrate the concept and drive discussion: