对任何django模型的webnotice支持。
django-wm的Python项目详细描述
提及
使用“提及”可以将Webmention功能添加到任何django模型中 设置。有一个在https://django-wm.dev/运行的源代码为 代码可用here。
安装
pip install django-wm
设置
芹菜
使用Celery和RabbitMQ异步运行任务。 如果您还没有使用它们,您需要先设置它们。
系统:
sudo apt install rabbitmq-server # Create user for celery service and make sure it can access your database # e.g: sudo useradd -N -M --system -s /bin/bash celery sudo usermod -a -G www-data celery # Run celery sudo -u celery env/bin/celery -A projectname worker &
项目:
Add celery.py
to your root project. For an example implementation
see celery.py from the example project.
项目代码
根项目
settings.py
:Set
DOMAIN_NAME
:DOMAIN_NAME = 'your.url.here' # e.g. 'django-wm.dev'
Add “mentions” to
INSTALLED_APPS
:INSTALLED_APPS = [ 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.flatpages', 'django.contrib.sites', ... 'mentions', ]
Add :code:’mentions.middleware.WebmentionHeadMiddleware’ to
MIDDLEWARE
:MIDDLEWARE = [ ... 'mentions.middleware.WebmentionHeadMiddleware', ]
根项目
urls.py
urlpatterns = [ ... path('webmentions/, include('mentions.urls')), ]
在要支持的模型中包含
重要的是:任何包含混音的模型也必须 实现MentionableMixin
WebNotice功能。all_text
和get_absolute_url
方法:from mentions import MentionableMixin ... class MyModel(MentionableMixin, models.Model): ... def all_text(self) -> str: return f'{self.introduction} {self.content}' def get_absolute_url() -> str: return reverse('app.views.name', kwargs={'slug': self.slug})
更新数据库表:
python manage.py makemigrations python manage.py migrate
可选设置
将这些键添加到项目settings.py
中以更改默认行为。
WEBMENTIONS_AUTO_APPROVE
=bool
(default:False
)
True
: Received webmentions are automatically approved and may be publicly visible.False
: Received webmentions require manual approval before they can be made public.