Login

simple guestbook

Author:
laxu
Posted:
August 25, 2008
Language:
Python
Version:
.96
Score:
1 (after 5 ratings)

a simple guestbook. the guestbook-area and entries can (and should) be styled by CSS. The template extends a "base.html", which should contain a "content" block.

The Model is very simple without moderation, admin-comment or any other advanced features, but its easy to extend.

i.e. add a Field "active=models.BooleanField()" and add "exclude=['active']" to the forms.EntryForm.Meta class for moderated Entries. Now you can switch the entries on/off in the admin-interface by setting active=True/False

this snippet is public domain, use for everything you want.

UPDATE: added basic SPAM protection (a do_not_fill field), but you might want to try a captcha-form/Field like snippet 812

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#guestbook/forms.py
#
from django.forms import ModelForm, CharField, ValidationError
from guestbook.models import Entry

class EmptyField(CharField):
	def clean(self, value):
		if value!='':
			raise ValidationError('Please DO NOT fill this field!')
class EntryForm(ModelForm):
	do_not_use=EmptyField(label="Do not fill")
	def __init__(self, data=None, *args, **kwargs):
		if data=={}:
			data=None
		ModelForm.__init__(self, data, *args, **kwargs)
	class Meta:
		model=Entry
#guestbook/models.py
from django.db import models
from django.contrib import admin

class Entry(models.Model):
	nick=models.CharField(max_length=50)
	url=models.URLField(blank=True)
	date=models.DateTimeField(auto_now_add=True)
	text=models.TextField()
try:
	admin.site.register(Entry)
except admin.sites.AlreadyRegistered:
	pass
#guestbook/views.py
from django.shortcuts import render_to_response
from guestbook.models import Entry
from guestbook.forms import EntryForm

def guestbook(request):
	form=EntryForm(request.POST)
	if form.is_valid():
		form.save()
	entries=Entry.objects.all().order_by("-date")
	templates={'form': form, 'entries': entries}
	return render_to_response("gbook.html", templates)
#guestbook/templates/gbook.html
{% extends "base.html" %}
{%block content%}
<h1>Guestbook</h1>
<form action="" method="POST">
<table>
{{form}}
</table>
<input type="submit" value="send" />
</form>
<div class="guestbook">
{%for entry in entries %}
<div class="entry">
<div class="header">
{%if entry.url%}<a href="{{entry.url}}">{%endif%}
<span class="nick">{{entry.nick}}</span>
{%if entry.url%}</a>{%endif%} 
wrote at
<span class="date">{%load humanize%}{{entry.date.date|naturalday}}</span>
<span class="time">{{entry.date.time}}</span>:
</div>
<div class="text">
{{entry.text|linebreaks}}
</div>
</div>
{%endfor%}
</div>
{%endblock%}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

laxu (on August 26, 2008):

hi!

if you voted this down, please tell me why.

Only if i know what the problem is, i can help/fix it. Thank you in advance.

#

sudheera (on August 31, 2008):

nice example,

have to enable django.contrib.humanize

btw, check this line, line break missing : admin.site.register(Entry)from django.template import RequestContext

#

laxu (on September 1, 2008):

This line was a mistake pasting the script, from my more complex application using some RequestContext filling Middleware.

With direct_to_template you do not need a RequestContext in your servlets anyway.

#

kilian (on July 14, 2011):

hey, didnt work for me... it shows an Csrf error .. I added 'django.middleware.csrf.CsrfResponseMiddleware', to middleware classes...then it shows another error..."'EntryForm' object has no attribute 'is_bound'" dont know what to do :/

#

laxu (on August 2, 2011):

When using the csrf-middleware, you need to add {%csrf_token%} to your form.

#

mbrochh (on March 19, 2012):

@killian you need to call super() in the init method of the form. I guess this snipped is faulty.

#

laxu (on March 23, 2012):

ModelForm.init(self, data, args, *kwargs) is (here) the same as using super(Modelform, self).init, but super may be the better choice.

#

Please login first before commenting.