Skip to content

adilkhash/django-trix-editor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Trix Editor Integration

This package provides a Django app that integrates the Trix editor with your Django project. Requires Django 4.1+

Installation

Install the package with pip:

pip install django-trix-editor

To change the version of Trix that is used, you can specify the version in your settings.py:

TRIX_VERSION = '2.0.0'

Usage

To use this package, you need to add it to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'trix_editor',
    ...
]

Don't forget to add the trix_editor urls to your urls.py to handle attachments:

from django.urls import include, path

urlpatterns = [
    ...
    path('trix-editor/', include('trix_editor.urls')),
    ...
]

If you want to give a specific permission to upload attachments, you can specify it in your settings.py:

TRIX_UPLOAD_PERMISSION = 'your_model.upload_attachment'

You can use the TrixEditorField in your models:

from django.db import models
from trix_editor.fields import TrixEditorField

class MyModel(models.Model):
    content = TrixEditorField()

Templates and forms

Customize your forms to use the widget TrixEditorWidget:

from django import forms
from trix_editor.widgets import TrixEditorWidget

class MyForm(forms.Form):
    content = forms.CharField(widget=TrixEditorWidget())

But don't forget to include the following statements to your template to load the Trix editor assets:

...
<head>
    <meta charset="utf-8">
    <title></title>
    ...
    {{ form.media.css }}
</head>
...
<!-- footer -->
{{ form.media.js }}

Django Admin Integration

To use the Trix editor in the Django admin, you need to add the following to your admin.py:

from django.contrib import admin
from django import forms

from trix_editor.widgets import TrixEditorWidget

class ContentForm(forms.ModelForm):
    class Meta:
        model = Content
        fields = ["title", "content", "status"]
        widgets = {
            "content": TrixEditorWidget(),
        }

@admin.register(Content)
class ContentAdmin(admin.ModelAdmin):
    list_display = ("title", "status", "created", "updated")
    form = ContentForm