Package: django-sirtrevor
django-sirtrevoris a simple Django app that provides a content editing
widget based on the fantasticSir Trevorproject.Quick startInstall django-sirtrevor:pip install django-sirtrevorAddsirtrevorto your INSTALLED_APPS setting like this:INSTALLED_APPS = (
    ...
    'sirtrevor',
)Add sir trevor urls:url(r'^sirtrevor/', include('sirtrevor.urls')),Create a model that makes use ofSirTrevorField:from django.db import models
from sirtrevor.fields import SirTrevorField

class MyModel(models.Model):
    ...
    content = SirTrevorField()
    ...Now you can …see it in action in the Django admincreate aModelFormfrom your modelcreate a plainFormand usesirtrevor.forms.SirTrevorFormFieldusesirtrevor.widgets.SirTrevorWidgetas a widget replacement for aTextareaConfigurationSir Trevorhas a fewconfiguration options. You can customize most of
them project-wide in yoursettings.pyor some on a per-widget basis askwargsforSirTrevorWidget.Available options(CONFIGURATION_SETTINGS/widget_kwargs):SIRTREVOR_BLOCK_TYPES/st_block_typesA list of block types to use with the editor.
Defaults to['Text', 'List', 'Quote', 'Image', 'Video', 'Tweet', 'Heading']SIRTREVOR_DEFAULT_TYPE/st_default_typeThe default block to start the editor with.
Defaults toNoneSIRTREVOR_BLOCK_LIMIT/st_block_limitThe overall total number of blocks that can be displayed.
Defaults to0SIRTREVOR_BLOCK_TYPE_LIMITS/st_block_type_limitsLimit on the number of blocks that can be displayed by its type.
Defaults to{}SIRTREVOR_REQUIRED/st_requiredMandatory block types that are required for validatation.
Defaults toNoneSIRTREVOR_UPLOAD_URL/st_upload_urlURL for AJAX image uploads.
Defaults to/sirtrevor/attachments/(depending on where you include
django-sirtrevor’s URLs inurls.py)SIRTREVOR_UPLOAD_PATHPath where to store uploaded images relative toMEDIA_ROOT. (not
configurable via widget kwargs)
Defaults toattachmentsSIRTREVOR_ATTACHMENT_PROCESSORA string containing a dotted path to a function that will be run before
saving an uploaded image. Seebelowfor more details. (not configurable via
widget kwargs)
Defaults toNoneResizing ImagesYou can resize uploaded images by implementing a function somewhere in your
code and pointingSIRTREVOR_ATTACHMENT_PROCESSORto its location. The first
argument will be the file object and the method must return aSimpleUploadFileobject.Example implemented inutils.pyin an app calledcore.SIRTREVOR_ATTACHMENT_PROCESSORset tocore.utils.resize_attachment:from PIL import Image
from StringIO import StringIO
from django.core.files.uploadedfile import SimpleUploadedFile


def resize_attachment(file_):
    size = (1024, 9999)
    try:
        temp = StringIO()
        image = Image.open(file_)
        image.thumbnail(size, Image.ANTIALIAS)
        image.save(temp, 'jpeg')
        temp.seek(0)
        return SimpleUploadedFile(file_.name, temp.read(), content_type='image/jpeg')
    except Exception as ex:
        return file_Custom BlocksSir Trevorcan be extended throughcustom blocks. Starting with 0.2.1django-sirtrevoralso has basic support for custom blocks.Here is a little step-by-step guide:myapp/blocks.py:from sirtrevor.blocks import BaseBlock

class MyCustomBlock(BaseBlock):
    name = 'MyCustomName'

    class Media:
        js = ['sirtrevor/blocks/mycustomblock.js']myapp/models.py:import sirtrevor
from .blocks import MyCustomBlock

sirtrevor.register_block(MyCustomBlock)myapp/static/sirtrevor/blocks/mycustomblock.js:SirTrevor.Blocks.MyCustomName = SirTrevor.Block.extend({
    type: 'mycustomblock',
    // ...
});Please refer toSir Trevor’s docs regardingcustom blocksfor details
about the JavaScript part of a custom block.myapp/templates/sirtrevor/blocks/mycustomblock.html:<div class="content-block mycustom-block">
    <!-- Whatever JSON the custom block creates is available in the template -->
</div>settings.py:# ...

SIRTREVOR_BLOCK_TYPES = ['Text', '...', 'MyCustomName']

# ...For reference please check outdjango-sirtrevor-filewhich implements a
simple block type for file downloads.LicenseMIT