Dec

8

Free Previews

My friends, I have something exciting (and completely unnecessary) in the works.

To pave the way for all the stuff I want to say about that, I've added previewing to my blog.

First, I added a preview field to BlogPost:

preview = models.TextField(_('preview'), blank=True)

This required another evolution of the db:

MUTATIONS = [
AddField('BlogPost', 'preview', models.TextField, initial='')
]

Then I added some logic to the body outputting part of my blogpost item template:

{% if preview and blogpost.preview %}
{{ blogpost.preview|markdown }}
<p><a href="{{ blogpost.get_absolute_url }}">More...</a></p>
{% else %}
{{ blogpost.body|markdown }}
{% endif %}

Then I just had to set preview to True in all the blog listing views. Here's how the home view looks (the rest are similar:

def home(request):
return date_based.archive_index(
request,
BlogPost.objects.all(),
date_field='pub_date',
template_name='blog/home.html',
template_object_name='blogpost_list',
extra_context={'preview': True}
)