Skip to main content

Yet another bookmarking library (yabl) for Django. Bookmark and tag arbitrary models.

Project description


Yet another bookmarking library (yabl) for Django. Bookmark and tag arbitrary models.


The AbstractBookmarkable contains a bookmarks field mapped to a generic Bookmark model containing:

  1. the authenticated user adding the bookmark, i.e. the bookmarker;
  2. the concrete model instance referenced, i.e. the bookmarked;[^1] and
  3. a ManyToMany tags field which maps to a TagItem model.

[^1]: The model is referenced via a content_type and an object_id

This abstraction makes each instance bookmarkable and taggable by users.

Specifically the following attributes are enabled:

Attribute Purpose
is_bookmarked(user) Check whether object instance is bookmarked or not
get_bookmarked(user) Get instances of model that user has bookmarked
get_user_tags(user) If user bookmarked, get user-made tags on instance
toggle_bookmark(user) Toggle bookmark status as bookmarked or not
add_tags(user, tags: list[str]) Add unique tags, accepts a list of names
remove_tag(user, tag: str) Delete an existing tag name from tags previously added
set_bookmarked_context(user) Combines relevant urls and attributes for template output
@modal Custom modal enables: toggle, add tags, remove tag
@launch_modal_url URL to launch custom modal
@add_tags_url URL to POST tags added
@del_tag_url URL to DELETE tag added
@toggle_status_url URL to toggle bookmark status of an object instance added
@object_content_for_panel Content when custom modal is loaded; must be overriden


  1. django-allauth setup
  2. htmx basics
  3. Overriding Django templates

Initial setup

Install in virtual environment

.venv> pip3 install django-allauth # for authentication purposes only
.venv> pip3 install django-bookmark-and-tag # poetry add django-bookmark-and-tag

Add to project settings

    # for authentication purposes only
    # add the following:
    "bookmarks", # new
    "examples" # your app that will be bookmarked / tagged

Setup django-allauth

A basic configuration just to enable easy access to authentication urls / views / templates:

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
ACCOUNT_LOGOUT_ON_GET = True  # no need to confirm logout

Add templates root directory:

        "DIRS": [BASE_DIR / "templates"],  # added

Create a file under this specific directory to override allauth defaults:

<!-- templates/account/base.html -->
{% extends 'base.html' %}
{% block title %} Authentication | BrandX {% endblock title %}

<!-- This overrides allauth /templates base.html -->

{% block content %}
{% endblock content %}

Add bookmarks urls

urlpatterns = [
    path("accounts/", include("allauth.urls")), # for ensuring authentication only
    path("bookmarks/", include("bookmarks.urls")), # new
    path("", include("examples.urls")), # 'examples.urls' is just for purposes of testing this ought to be replaced by the app whose model will be bookmarked / tagged

Run migration

.venv> python migrate

Run tests

.venv> pytest --ds=config.settings --cov

Optional fixtures

Sample fixtures can be loaded into the SampleBook and SampleQuote model found in examples/

>>> python loaddata bookquotes.yaml # fixtures which show sample books

Backend Configuration

Let's assume an examples app containing a SampleBook model:


Add mixin to target model

Add an AbstractBookmarkable mixin to the

# examples/
class SampleBook(AbstractBookmarkable):

Add verbose name for contenttypes

Add a verbose_name to Meta.options so that if referenced via its Content_Type .name, the assigned verbose_name appears.

# examples/
class SampleBook(AbstractBookmarkable):
    class Meta:
        verbose_name = "Book"  # see generic relations, e.g.
        verbose_name_plural = "Books"

Add mixin for attributes

Each book instance obj has access to @properties and methods requiring a user:

Customize display of model in panel

Must declare an @object_content_for_panel in the inheriting model.

Each display will be different because each model will have different fields / different html markup. Instead of rendering a separate template per model, can customize a model attribute via format_html():

# examples/
class SampleBook(AbstractBookmarkable):
    def object_content_for_panel(self) -> SafeText: # customizes appearance of a specific book within the custom modal
        return format_html(
            author=f"{} {}",

Configure (copy/paste)

Copy and paste a preconfigured set of views, matching the same to another model, e.g. ArbitraryPainting. See sample set of views using a SampleBook model:

# examples/
from bookmarks.utils import Pathmaker, MODAL_BASE, PANEL

def launch_modal_samplebook(request: HttpRequest, pk: str) -> TemplateResponse: # change view name
    obj = get_object_or_404(SampleBook, pk=pk) # change model to ArbitraryPainting
    panel = {"content_template": PANEL}
    context = obj.set_bookmarked_context(request.user) | panel
    return TemplateResponse(request, MODAL_BASE, context)

def toggle_status_samplebook(
    request: HttpRequest, pk: str
) -> TemplateResponse: # change view name
    obj = get_object_or_404(SampleBook, pk=pk) # change model to ArbitraryPainting
    context = obj.set_bookmarked_context(request.user)
    return TemplateResponse(request, PANEL, context)

def add_tags_samplebook(request: HttpRequest, pk: str) -> TemplateResponse: # change view name
    obj = get_object_or_404(SampleBook, pk=pk) # change model to ArbitraryPainting
    if submitted := request.POST.get("tags"):
        if add_these := submitted.split(","):
            obj.add_tags(request.user, add_these)
    context = obj.set_bookmarked_context(request.user)
    return TemplateResponse(request, PANEL, context)

def del_tag_samplebook(request: HttpRequest, pk: str) -> HttpResponse: # change view name
    obj = get_object_or_404(SampleBook, pk=pk) # change model to ArbitraryPainting
    if delete_this := request.POST.get("tag"):
        obj.remove_tag(request.user, delete_this)
    return HttpResponse(headers={"HX-Trigger": "tagDeleted"})

Each view function from app/`` related to a particular model should be imported into app/``. The views can be consolidated to a single pattern based on a `Pathmaker` helper dataclass defined in bookmark/
BOOK = Pathmaker(
    model_klass=SampleBook, # change model to ArbitraryPainting
    launch_func=launch_modal_samplebook, # add changed view name
    toggle_status_func=toggle_status_samplebook, # add changed view name
    add_tags_func=add_tags_samplebook, # add changed view name
    del_tag_func=del_tag_samplebook, # add changed view name

The copy/pasting isn't the most elegant solution but it provides better understanding of the data flow.


Declare a's app_name. The value declared must match the model object's ._meta.app_label.

So in the SampleBook model example above, since model is contained in the examples app, can configure the likeso:

# examples/
from .views import SAMPLEBOOK
app_name = "examples" #  = SampleBook.objects.get(pk=1)._meta.app_label
urlpatterns = (SAMPLEBOOK.make_patterns())

The reason for the matching requirement is that django.urls.reverse() functions will rely on this convention to call urls from the object instance with a pre-determined property value, e.g.:

>>> book_obj = SampleBook.objects.first()
>>> book_obj.launch_modal_url
>>> quote_obj = SampleQuote.objects.first()
>>> quote_obj.launch_modal_url

Override tags/tags.html

In the examples app, we declared two AbstractBookmarkable models: SampleBook and SampleQuote.

The view annotated_tags() effectively determines implemeting classes of AbstractBookmarkable and produces the proper annotations.

Still exploring best way to display this annotated list of tags in the template.

For now, can override the tags/tags.html template, replacing samplebook and samplequote, the verbose model names of the implementing classes, with your selected models.

{% for tag in tags %}
    ...<!-- Do this for each model that you want to show a count of tags for -->
    {% include './item.html' with count=tag.samplebook_count slug=tag.samplebook_slug idx=tag.samplebook_id %}
    {% include './item.html' with count=tag.samplequote_count slug=tag.samplequote_slug idx=tag.samplequote_id %}
{% endfor %}

Frontend Customization

Basic design

  1. The base.html uses the htmx/hyperscript example modal.css and a bespoke starter.css declared in an app-level static folder.
  2. The app-level modal.html displays custom modals via htmx click.
  3. The app-level panel.html provides content displayed within custom modals.
  4. The modal is where backend actions – i.e. toggle bookmark status, add tags, remove tag – become operational.

Overriding style

  1. Modify base.html to use [insert framework here].
  2. Declare root-level templates directory, add a subdirectory tags/ with a panel.html
  3. Copy and paste the app-level panel.html into the directory created in (2.)
  4. Style the root-level panel.html based on inserted framework.

UX via htmx & hyperscript

  1. Load custom modal based on htmx custom modal css style:

    <!-- adds a custom DOM element before the end of the body, adding a dark underlay to the DOM to highlight the modal's contents -->
    <em hx-trigger="click"
  2. Post data on checkbox change:

    <!-- 'closest section' assumes a parent container <section> -->
            hx-target="closest section"
            {% if is_bookmarked %}
            {% endif %}
  3. Delete from backend and remove frontend element on click

    <!-- badge <span>x</span> for easy deletion; when clicked, a DELETE request is sent to the backend. Once deleted, the frontend receives a trigger "tagDeleted" to remove the DOM element targeted by deletion. -->
    <small id="del_this">
        Thing to delete
            hx-confirm="Are you sure you want to delete this?"
            _="on tagDeleted transition opacity to 0 then remove #del_this"

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-yabl, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size django_yabl-0.0.1-py3-none-any.whl (66.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-yabl-0.0.1.tar.gz (61.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page