Skip to content

Guide_Getting started

Michael Beaton edited this page Nov 25, 2022 · 2 revisions

Overview

  1. Install django-wm
  2. Add required settings
  3. Update project urlpatterns
  4. Add the mixin to your model(s)
  5. Update app urlpatterns
  6. Update migrations
  7. Test it!

Installation

PyPI: django-wm

With celery

pip install django-wm[celery]

Please follow these instructions to set up Celery, then come back here for the rest.

Without celery:

pip install django-wm

Please follow these instructions, then come back here for the rest.

Project code

For reference, source code for an example project is available here.

Add required settings:

In settings.py:

  • Set DOMAIN_NAME:

DOMAIN_NAME = "your.url.here" # e.g. "beatonma.org" ```

  • Add mentions to INSTALLED_APPS:

INSTALLED_APPS = [ ... "mentions", ] ```

MIDDLEWARE = [ ... "mentions.middleware.WebmentionHeadMiddleware", ] ```

Project urls.py:

# yourproject/urls.py

urlpatterns = [
        ...
        path("webmentions/", include("mentions.urls")),
]

Models

Include MentionableMixin in the model(s) you want to support webmention functionality.

Any models that include the mixin must implement the get_content_html and get_absolute_url methods:

# models.py

from mentions.models.mixins import MentionableMixin

class Article(MentionableMixin, models.Model):
content = models.TextField()

def get_content_html(self) -> str:
    return self.content

def get_absolute_url(self) -> str:
    return reverse("article", args=[self.id])

App urls.py

Add urlpatterns metadata for any views that represent a MentionableMixin model instance. The mentions_path helper is the easiest way to do this - see here for details and further configuration options.

# your_app/urls.py

from mentions.helpers import mentions_path

urlpatterns = [
    mentions_path(
        "article/<int:article_id>/",
        ArticleView.as_view(),
        model_class=Article,
        model_filter_map={
            "article_id": "id",
        },
        name="article",
    ),
]

Update migrations:

python manage.py makemigrations
python manage.py migrate

Test it!

This page accepts Webmentions and lets you send one to yourself, so you can check that both incoming and outgoing Webmentions work on your server.