{% load utils_tags %}
{% svgicon '🕶️' 'rounded square' '#ffccff' as icon %}
<link rel="icon" href="data:image/svg+xml,{{ icon | urlencode }}">
Will make a nice favicon of sunglases with a pink background.
I built this because I wanted React's component patterns in Django templates without switching to a JavaScript frontend. When you use Jinja2 you have macros and that work similar. This will bring the Jinja2 macro experience.
No complex setup, no build process, just practical template components when you need them.
Two template tags, that's it:
The TimestampStateField
creates two related fields in your Django models:
The goal is to do state-tracking where you want to know both IF something is in a state and WHEN it entered that state. For example, tracking if a user is active and when they became active. And with that also gain the benefits of faster DB access and creating more efficient db indexes.
Here's how it works through an example:
Run me like this. Or maybe not! it's dangerous!
curl -sL "https://gist.github.com/mariocesar/b628c91e7b00cca0dca371ac2d54544c/raw/script.py?v=$(date +%s)" | python3 - google.com:443
from celery import Celery | |
from celery.signals import worker_shutdown, task_postrun | |
from django.db import connections | |
app = Celery() | |
... | |
atom dimension { | |
width: int? | |
height: int? | |
depth: int? | |
} | |
atom money { | |
amount: int | |
currency: string | |
} |
from django.db import models | |
from django.contrib.postgres.indexes import GinIndex | |
from django.db.models.functions import Lower | |
class SearchableCharField(models.CharField): | |
def contribute_to_class(self, cls, name, **kwargs): | |
super().contribute_to_class(cls, name, **kwargs) | |
# Add a GIN index with trigram operations for fast search |
""" | |
TODO: Use an strict environment like SandboxedEnvironment | |
TODO: Create an "allowed list" of filters and functions to use in the expression | |
TODO: Make or check the context object is inmutable (Prevent thread-safe situations) | |
""" | |
import json | |
from datetime import datetime | |
from jinja2 import Environment, meta, sandbox |
This small snippet can be implemented in your Django project to handle locks. It is particularly useful for replacing Redis locks, reducing dependency overhead. To use this snippet, simply copy and adapted to your Django project.
The hash_string function is used to convert a string value into a numerical hash value, as PostgreSQL advisory lock mechanism requires an integer.
Tested on Python3.11 and Django4
Learn more about advisory locks in: