Skip to main content

Provides a TextFormatField for Django models and allows to build text formats out of simple filters.

Project description


pypi-badge build-status

django-textformat makes it easy to configure and compose text formats. A text format is simply a set of text filters that are applied in a defined order. A text filter is a function that takes a markup string and returns a the formatted string.

Built in filters are:


It’s the same as Django’s escape template filter, available as text filter.


It’s the same as Django’s linebreaks template filter, available as text filter.


It’s the same as Django’s linebreaksbr template filter, available as text filter.


It’s the same as Django’s striptags template filter, available as text filter.


It’s the same as Django’s urlize template filter, available as text filter.

django-textformat does not provide any text formats by default, but to give you an idea, a text format might consist of the builtin filters html_escape, urlize, linebreaks_and_paragraphs.

That would allow you to group those filters into a format and use it consistently throuhgout your project.


The default use case for django-textformat might look like this in your model:

from django.db import models
from django_textformat import TextFormatField

class Article(models.Model):
    title = models.CharField(max_length=50)
    content = models.TextField()
    content_format = TextFormatField()

Then you can use the selected format for the article in your template like this:

{% load textformat %}

{{ article.content|apply_format:article.content_format }}

Initially creating text formats

In order to use a model like Article above, you already need to have a django_textformat.models.TextFormat instance defined. You can either create the format by hand or use a data migration. We suggest using a data migration which will make sure that all instances of your project (e.g. for all devs) have the same formats available.

To do so, create an empty migration in one of your websites apps, like:

python makemigrations blog --empty

Now make the newly created migration look something like this:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

def add_format(apps, schema_editor):
    TextFormat = apps.get_model('django_textformat', 'TextFormat')

    markdown_format = TextFormat.objects.create(
        name='Article Format')

def remove_format(apps, schema_editor):
    TextFormat = apps.get_model('django_textformat', 'TextFormat')

    format = TextFormat.objects.get(slug='article')

class Migration(migrations.Migration):

    dependencies = [
        ('django_textformat', '0001_initial'),

    operations = [

Adding custom text filters

It’s easy to add custom text filters. In order to add one, you need a file in your app. Then add a function that takes a string and returns the formatted string.

Here is an example:

# in your_app/

from django_textformat.registry import registry
import markdown

def markdown(value):
    return markdown.markdown(value, extensions=['extra'])

Now you have a text filter called 'markdown' available for use in your text formats.


Install the dependencies (including the test dependencies) with:

pip install -r requirements.txt

Then you can run all tests with:




  • Initial release.

Project details

Download files

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

Source Distribution

django-textformat-0.1.0.tar.gz (8.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page