simple guestbook

 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. Unobtrusive comment moderation by ubernostrum 7 years, 1 month ago
  2. AdminPeepingMiddleware by buriy 6 years, 7 months ago
  3. Redirect with change list with filters intact with admin actions by AndrewIngram 4 years, 9 months ago
  4. Add special field lookups to the Admin list_filter display by whiteinge 6 years, 2 months ago
  5. Generic CSV export admin action factory with relationship spanning fields and labels by blackrobot 1 year, 4 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.

#

(Forgotten your password?)