blog stats
seamusc.com

Django Wiki Part 2

Published on May 31, 2007

Building on the simple wikitags parser I wrote about earlier I'm now trying to build an application around that template tag.

A basic wiki should the following features:

  • Give the user the ability to create and edit pages.
  • Recent versions of a page should be stored to help see what has been changed and to mitigate the effects of spam.
  • The syntax of the markup/formatting language should be simple and allow easy linking, especially internal linking.
  • Have the ability to contain static pages

The django.views.generic.create_update module looks like the obvious choice to use for creating/editing pages but the basic structure will be something like this.

WikiPage model
webapp/wiki/models.py

import datetime

class WikiPage(models.Model):
    title = models.CharField(maxlength=100)
    content = models.TextField()
    created_on = models.DateTimeField(editable=False)
    last_modified = models.DateTimeField(auto_now=True)
    slug = models.SlugField(prepopulate_from=("title",))    

    class Admin:
       pass

    def __str__(self):
       return self.title

    def save(self):
       if self.id == None:
          self.created_on = datetime.datetime.now()
       super(WikiPage, self).save()

urls.py

from webapp.wiki.models import *

updateobject = {
    'model' : WikiPage,
    'slug_field' : 'slug',
    'template_name' : 'wiki/edit.html',
    }


(r'^wiki/edit/(?P<slug>[^/]*)/$', 'django.views.generic.create_update.update_object', updateobject ),

(r'^wiki/(?P<slug>[^/]*)/$' ,'webapp.wiki.views.viewpage' ),


webapp/wiki.views.py

from webapp.wiki.models import *
from django.views.generic.create_update import *
def viewpage(request, slug='home' ):
    try:
       page = WikiPage.object.filter(slug=slug)
       update_object(WikiPage, slug_field='slug', slug=slug,template_name='wiki/page.html')
    except:
       create_object(WikiPage,template_name='wiki/create.html')


templates/wiki/page.html

{% extends "base.html" %}
{% load wikify %}
{% block title %}{{ object.title|escape }}{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<span class="lastedited"> <!-- change this -->
Last edited on {{ object.last_modified|date:"F y, J" }}
</span> 
<div class="content">
{{ object.content|wikify }}
</div>
{% endblock %}

Comments

Post a comment

Your name:

Comment: