一行掲示板どれくらいで出来るかな。

Hexaの日記 - ActiveRecordを使った、1行掲示板(変更点2)
http://d.hatena.ne.jp/Hexa/20061022/1161522439

RubyでCGIとActiveRecordを使って実装していたので、

Djangoでやってみた。

$ django-admin.py startproject hexa
$ cd hexa
$ python manage.py startapp message
$ mkdir templates
$ cd templates
$ mkdir message

settings.pyは省略

/hexa/message/urls.py

from django.conf.urls.defaults import *
from hexa.message.models import Message

infodict = dict(model=Message)
extra_context = dict(objects=Message.objects.all())

urlpatterns = patterns('django.views.generic.create_update',
    (r'^$', 'create_object', dict(infodict, extra_context=extra_context)),
)

/hexa/message/models.py

from django.db import models

class Message(models.Model):
    name = models.CharField(maxlength=32)
    message = models.CharField(maxlength=128)
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-date"]
        verbose_name_plural = "messages"

    class Admin:
        list_display = ["name", "message", "date"]

    def get_absolute_url(self):
        return "/message/"

/hexa/urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^message/', include('hexa.message.urls')),

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

/hexa/templates/message/message_form.html

掲示板
<form action="" method="post">
    <p><label for="id_name">名前:</label> {{ form.name }}</p>
    <p><label for="id_message">発言:</label> {{ form.message }}</p>
    <input type="submit" value="送信">
</form>

名前 内容
{% if objects %}
{% for object in objects %}
<hr />
{{ object.name }} : {{ object.message }}
{% endfor %}
{% endif %}

1行掲示板
1行掲示板 posted from フォト蔵

管理画面
管理画面 posted from フォト蔵