Django-inline-media is a simple reusable app that allows insertion of inline media content into TextFields.
You might find this application useful if you need to add inline content to a TextField. A blogging app is a good candidate. In general, any custom model in your project aimed to show text combined with inline media may benefit from it.
Django-inline-media comes with two media models: Picture and PictureSet, but you can create yours to support other media formats or providers.
This tutorial explains how to install and configure django-inline-media, how to integrate it in your web project and how to use the new widget.
Installing Django-inline-media is as simple as checking out the source and adding it to your project or PYTHONPATH.
Use git, pip or easy_install to check out Django-inline-media from Github or get a release from PyPI:
- Use git to clone the repository, and then install the package (read more about git):
- git clone git://github.com/danirus/django-inline-media.git and
- python setup.py install
- Or use pip (read more about pip):
- Do pip install django-inline-media, or
- Edit your project’s requirements file and append either the Github URL or the package name django-inline-media, and then do pip install -r requirements.
- Or use easy_install (read more about easy_install):
- Do easy_install django-inline-media
Configuration comprehends the following steps:
- sorl.thumbnail: http://pypi.python.org/pypi/sorl-thumbnail/
- tagging: http://pypi.python.org/pypi/tagging/
- Add inline_media, sorl.thumbnail and tagging to INSTALLED_APPS.
- Add THUMBNAIL_BACKEND = "inline_media.sorl_backends.AutoFormatBackend"
- Add THUMBNAIL_FORMAT = "JPEG"
- Optionally you can add an extra setting to control where django-inline-media stores images (see Settings), but it has a sane default.
Using inline-media is pretty straightforward:
Let’s see it with an example: the Article model.
The Article model, in the demo project, has a couple of fields of type TextField, abstract and body. Only the field body may have inline media content. Django-inline-media comes with a new field TextFieldWithInlines that extends Django’s TextField to support inline media content insertion. The new Article’s definition will use the new type for the body field:
from inline_media.fields import TextFieldWithInlines
class Article(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(unique_for_date='publish')
abstract = models.TextField()
body = TextFieldWithInlines()
publish = models.DateTimeField(default=datetime.now)
And the ArticleAdmin class will inherit from both, AdminTextFieldWithInlinesMixin and Django’s ModelAdmin:
from django.contrib import admin
from inline_media.admin import AdminTextFieldWithInlinesMixin
from demo.articles.models import Article
class ArticleAdmin(AdminTextFieldWithInlinesMixin, admin.ModelAdmin):
list_display = ('title', 'publish')
list_filter = ('publish',)
search_fields = ('title', 'abstract', 'body')
prepopulated_fields = {'slug': ('title',)}
fieldsets = ((None,
{'fields': ('title', 'slug', 'abstract', 'body',
'publish',)}),)
admin.site.register(Article, ArticleAdmin)
Four models are available when installing inline_media:
In order to insert inline content in your text fields you have to:
Look at the demo project admin site. See that Picture and PictureSet are already instances of InlineType. Then click on any of the articles admin change view and see that the inlines field below the body allows you to choose between inline types Picture and PictureSet:
Later when rendering articles detail (example/demo/templates/articles/article_detail.html) you have to load the inlines templatetag and apply the render_inlines filter to the body field:
{% load i18n inlines %}
...
<div class="inline_media_clearfix">
{{ object.body|render_inlines }}
</div>
And the filter will use the template inline_media/templates/inline_media/inline_media_pictureset.html to render the inline media.