July 11, 2011

Django Sort of Static Site Generator.

Built out on ServeLocally.org

We needed a really simple site on ServeLocally.org
We really like Django for it's template abilities, among dozens of other things.

I made a one-view django site that would render, cache and serve any of our templates.

There are no models, no DB, and you can cache with wild abandon. 

from django.conf.urls.defaults import patterns, url
from django.conf import settings
from django.http import Http404
from django.template import RequestContext
from django.shortcuts import render_to_response

def direct(request, path):
    try:
        return render_to_response("%s.html" % path[:-1], context_instance=RequestContext(request))
    except:
        raise Http404

urlpatterns = patterns('',
    # Just for Development
    url(r"^static/(?P<path>.*)$", "django.views.static.serve", {"document_root": settings.STATIC_ROOT}),
    
    # My New Django Wiki-Alike Thing
    url(r"(?P<path>.*)", direct)

 

)

That's my urls.py, and my one view.  It will serve any url that has an associated template, so 

http://servelocally.org/

is served by
templates/.html

http://servelocally.org/about/
is served by
templates/about.html

http://servelocally.org/features/sms/

(would actually throw a 404)

but if it were real, it would be served by
templates/features/sms.html

I like it a lot, it's pretty close to github's wiki system, feel free to use it.

 

Full code is on Github

©2011 Kelly Creative Tech. PO Box 82049. Columbus OH 43202. Toll Free: 888-794-6509