wiki20

まだ途中ですが…。

views.py

from django.shortcuts import render_to_response
from wiki20.root.models import Page
from django.http import HttpResponseRedirect, Http404

def index(request, pagename='FrontPage'):
    try:
        page = Page.objects.get(pagename=pagename)
    except Page.DoesNotExist:
        raise Http404

    return render_to_response('root/page.html', dict(page=page))

def edit(request, pagename):
    page = Page.objects.get(pagename=pagename)
    
    return render_to_response('root/edit.html', dict(page=page))

def save(request):
    pagename = request.POST['pagename']
    data = request.POST['data']
    
    page = Page.objects.get(pagename=pagename)
    page.data = data
    page.save()
    
    return HttpResponseRedirect('/%s' % pagename)

def pagelist(self):
    pages = [page.pagename for page in Page.objects.order_by('pagename')]
    
    return render_to_response('root/pagelist.html', dict(pages=pages))

models.py

from django.db import models

class Page(models.Model):
    pagename = models.CharField(primary_key=True, maxlength=30)
    data = models.TextField()
    
    class Admin:
        pass

urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^$', 'wiki20.root.views.index'),
    (r'^(?P<pagename>.+)/edit/$', 'wiki20.root.views.edit'),
    (r'^save/$', 'wiki20.root.views.save'),
    (r'^pagelist/$', 'wiki20.root.views.pagelist'),

    (r'^admin/', include('django.contrib.admin.urls')),

    (r'^(?P<pagename>.+)/$', 'wiki20.root.views.index'),
)