Simple web sites with django == win

Last night I put the main benogle.com site up. I have been looking for an easy way to create a simple website for a while. All I wanted was a couple things: quick setup with low cruft, pages editable from the browser, and quick, easy extension with god like power. Making a website is such a common occurrence, you’d think there would be a whole heap of options available that are stupid easy to use. I bet you are thinking, “gee, there are tons of CMS apps out there!” You’re right. I have used Typo3, textpattern, WordPress, and I have evaluated even more of them. Each one has been too heavy. They want to do too much in all the wrong places. On top of that, extension requires digging through their obscure plugin API and scouring their forums for something that should be simple. Oh, the pain.

I am happy to say that I have finally found a solution: Django. While I am sure it excels in building large-scale apps, it has been a completely perfect tool for creating a simple website such as benogle.com. I am surprised that, despite having never used Django before, it took a little over 6 hours (spread over several days) to construct, plus a couple hours for writing content. Wow! It took longer to build the theme for this WordPress blog! I recently built a site with about the same complexity for a family member with textpattern. I have four to five times more time and effort in it.

Why does it fit so well? Three reasons:

  • My web dev skills transfer in that I don’t have to learn a really complex plugin/theme architecture. Django’s template language is simple. I make templates. My views call them. Yessss.
  • I add only the stuff I need. With the blog and CMS packages, I spend soooo much time tying to figure out how to hide data I don’t care about or add a little extra data I do. With Django, I just create a model with the data I need. syncdb. Done.
  • The instant admin interface. Oh, it knows about my foreign keys and presents choices in a dropdown? Epic. Cruft? None.

The best part it is that there is soooo little code. I’ve made a public git repo containing said code. Included is a Django app called simplesite which has all the ‘business code’ goodness.

The site has multiple pages (no, really), and those pages may have a different header image, left-hand fluff image, and sidebar. So I needed a Sidebar table, an Image table, and a Page table. Here are the models:

class Sidebar(models.Model):
    name = models.CharField(max_length=30)
    content = models.TextField()
    
    def __unicode__(self):
        return self.name
    
    def gethtml(self):
        return textile.textile(self.content)
    
class Image(models.Model):
    name = models.CharField(max_length=30)
    filename = models.CharField(max_length=50)
    
    def __unicode__(self):
        return self.name
        
    def geturl(self):
        return '%s%s' % (settings.IMAGE_URL, self.filename)
    
class Page(models.Model):
    title = models.CharField(max_length=30)
    slug = models.CharField(max_length=100)
    header_image = models.ForeignKey(Image, related_name="headers")
    accent_image = models.ForeignKey(Image, related_name="accents")
    sidebar = models.ForeignKey(Sidebar)
    meta_description = models.CharField(max_length=256)
    meta_keywords = models.CharField(max_length=100)
    content = models.TextField()
    
    def __unicode__(self):
        return self.title
        
    def gethtml(self):
        return textile.textile(self.content)

Cool. Now we need code to display pages from model. This is the really easy part.

def index(req):
    page = Page.objects.get(id=settings.INDEX_PAGE_ID)
    return render_to_response('index.html', {'page': page})
    
def page(req, page_slug):
    page = get_object_or_404(Page, slug=page_slug)
    return render_to_response('page.html', {'page': page})

And the template that displays a regular page:

{% block content %}
<div class="entry-page">
	<div class="entry-image">
		<img src="{{ page.accent_image.geturl }}" alt="fluff image"/>
	</div>

	<div class="entry">
		<h2 class="entry-title">{{ page.title }}</h2>
		<div class="entry-content">
			{{ page.gethtml|safe }}
		</div>

	</div>
</div>
{% endblock %}

Done! Ok, well, I’ve left out the contact form handling code and the url map list. The rest of the work was in translating my static HTML template to a Django template. Oh man, do I like their template language. The inheritance makes for clean HTML, and the filters keep the garbage out of the templates.

What can I say, I am loving Django. I will be using the simplesite app, or some clone of it, for any more simple sites I build from now on. I have even started writing another, more complicated, application with Django just to try an explore the power and awesomeness. It feels so great to find a library that just ‘clicks.’

comment

Your email is never shared because I like you.

*
*