Simon Griffee
Department of graphic design, art direction, and photography.

June 2016

Covent Garden, London, UK, April 2008

Published 2016 June 24

Covent Garden, London, UK, April 2008

Published 2016 June 24

Covent Garden, London, April 2008

Published 2016 June 24

Script to Convert Django Flatpages to Hugo Markdown Files with YAML Frontmatter

I needed to export a lot of entries stored in the SQLite database of my previous Django site for importing into the new Hugo-powered SimonGriffee.com.

Thankfully, Sam Kingston had already written a script to export Mezzanine blog posts to Jekyll, so I modified it to make djangoflatpage2hugo.py, below.

To use it:

  1. Save the the code below in a file called djangoflatpage2hugo.py and put it in the following location:
    yourdjangoproject/
                 management/
                         commands/
                                 djangoflatpage2hugo.py
    .
  2. Make sure there are __init__.py files in both the management and commands folders.
  3. Run the following in your terminal: python manage.py djangoflatpage2hugo /chosen/output/directory/.
  4. Find the converted .md files in the /chosen/output/directory.
# _*_ coding:utf-8 _*_
from django.core.management.base import BaseCommand
from django.contrib.flatpages.models import FlatPage
import os
import re
from django.utils.text import slugify
# Thanks to http://stackoverflow.com/a/15364584/412329 for:
from django.utils.encoding import smart_str, smart_unicode

class Command(BaseCommand):
    """ 
    Usage: 
    1. Put this file in: 
    yourdjangoproject/
                management/
                        commands/
                                djangoflatpage2hugo.py
    2. Make sure there are __init__.py files in both management and commands folders
    3. Run: python manage.py djangoflatpage2hugo /chosen/output/directory/
    4. Find the converted .md files in /chosen/output/directory
    """
    help = 'Export hth flat pages as Hugo markdown files'

    def add_arguments(self, parser):
        parser.add_argument('output_dir', help='Output directory.')

    def handle(self, *args, **options):
        for page in FlatPage.objects.all():
            header = {
                'date': '2000-01-01',
                'title': page.title.replace(':', ''),
                # change /design/ to your section url
                'url': '/design/' + slugify(page.title) + '/',
            }
            
            output_dir = args[0]
            filename = '{slug}.markdown'.format(slug=slugify(page.title))
            
            content = page.content.encode('utf-8').replace('\r', '')
            
            # Write out the file
            with open(os.path.join(output_dir, filename), 'w') as fp:
                fp.write('---' + os.linesep)
                for k, v in header.items():
                    fp.write(smart_str('%s: %s%s' % (k, v, os.linesep)))
                fp.write('---' + os.linesep)
                fp.write(smart_str(content))

Booooomahhhh!

124th Street and St. Nicholas Avenue, Harlem, New York City, June 2016

Published 2016 June 22

42nd Street Near 8th Avenue, New York City, June 2016

Published 2016 June 22

Amber, Howard City, Michigan, June 2016

Published 2016 June 21

Bruce in Beacon, New York, June 2016

Published 2016 June 14

Washington Square South and LaGuardia Place, New York City, April 2016

Published 2016 June 13

Via dei Fori Imperiali, Rome, Italy, June 2011

Published 2016 June 12

Amsterdam Avenue Near 116th Street, New York City, August 2015

Published 2016 June 9

Bryant Park, New York City, May 2016

Published 2016 June 8

42nd Street and 5th Avenue, New York City, May 2016

Published 2016 June 7

Amber, Lastorta and Zucchina, Rome, Italy, September 2013

Published 2016 June 5

East 46th Street and Lexington Avenue, New York City, May 2016

Published 2016 June 5

5th Avenue, New York City, September 2015

Published 2016 June 4

Joshua Tree, California, January 2007

Published 2016 June 1

← Notebook Archive