A Greater Datetime

Django’s built in blog module is very cool.  Go back a few months’ entries on this blog and you’ll see that’s where I learned Django (and how most people do, I would think).  Powerful, well-documented, easy to extend- just a great thing to have available.

One of the features most blogs have these days is the ‘publish later’ options.  You can pre-create a bunch of entries, then have them post on certain days.  So it looks like I’m at my desk, being productive, while in reality I’m sitting on the beach, drink in hand.

Publish later isn’t built in to the default Django blog module, but it is pretty straightforward to add.  The default module does have a ‘date’ field- which allows you to set the date attached to the blog.  Why not use that?  Just update the view for your blog display to filter by the date field (set the filter to only show blogs with a date < today:

Blog.objects.filter(posted__lte=datetime.today()).order_by('-posted')

This also makes use of cool aspects of Django’s ORM- just throw a double underscore and ‘lte’ on a field and you have a SQL condition for ‘less that or equal to’.

There you go.  When creating a new blog entry, just set the date field (with built in calendar picker) to the date you want it to publish.  The entry is saved, but won’t display on your front end until the date you selected.

Simple?  Yes.  Impressive?  Probably not, but it was quick to set up and it works, so that’s good enough for me!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s