Commits

Cédric Bonhomme committed 6776340

Comments on posts.

Comments (0)

Files changed (7)

 # -*- coding: utf-8 -*-
 from cedricbonhomme.blog.models import Post
+from cedricbonhomme.blog.models import Comment
 from django.contrib import admin
 
+
 class PostAdmin(admin.ModelAdmin):
     fields = ['title', 'content_html', 'content']
 
-admin.site.register(Post, PostAdmin)
+admin.site.register(Post, PostAdmin)
+
+
+
+class CommentAdmin(admin.ModelAdmin):
+    display_fields = ["post", "author", "created"]
+
+admin.site.register(Comment, CommentAdmin)
 
 
 
+
     def __unicode__(self):
-        return self.title
+        return self.title
+
+
+class Comment(models.Model):
+    created = models.DateTimeField(auto_now_add=True)
+    author = models.CharField(max_length=60)
+    content = models.TextField()
+    post = models.ForeignKey(Post)
+
+
+
+    def __unicode__(self):
+        return unicode("%s: %s" % (self.post, self.content[:60]))

blog/templates/archives.html

     <div id="content">
     {% block content %}
         {% for e in entries %}
-            {{ e.created }} <a href="{% url blog.views.detail e.id  %}"> {{ e.title }} </a> <br/>
+            {{ e.created }} <a href="{% url blog.views.post e.id  %}"> {{ e.title }} </a> <br/>
         {% endfor %}
 
     {% endblock %}

blog/templates/detail.html

-{% extends "bbase.html" %}
-
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
-    <head>
-        <title>My Blog</title>
-    </head>
-
-<body>
-    <div id="content">
-    {% block content %}
-        {{ entry.content_html|safe }}
-    {% endblock %}
-    </div>
-</body>
-
-</html>

blog/templates/list.html

                 <ul>
                     <div class="time">{{ post.created }}</div>
                     <div class="body">{{ post.content_html|safe }}</div>
-                    <div class="commentlink"><a href="{% url blog.views.detail post.id  %}">Comments</a></div>
+                    <div class="commentlink"><a href="{% url blog.views.post post.id  %}">Comments</a></div>
                     <br />
                 </ul>
             {% endfor %}
 # Create your views here.
 from django.core.paginator import Paginator, InvalidPage, EmptyPage
 from django.shortcuts import render_to_response
+from django.core.context_processors import csrf
+from django.forms import ModelForm
+from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
+
 
 from cedricbonhomme.blog.models import Post
+from cedricbonhomme.blog.models import Comment
 
 
 def index(request):
 
     return render_to_response("list.html", dict(posts=posts, user=request.user))
 
-def detail(request, post_id):
-    """
-    Detail page. Display a post.
-    """
-    try:
-        entry = Post.objects.all()[int(post_id)-1]
-        return render_to_response('detail.html', locals())
-    except IndexError:
-        return render_to_response('list.html', Post.objects.all())
-    except AssertionError:
-        return render_to_response('list.html', Post.objects.all())
+
+def post(request, post_id):
+    """Single post with comments and a comment form."""
+    post = Post.objects.get(id=int(post_id))
+    comments = Comment.objects.filter(post=post)
+    d = dict(post=post, comments=comments, form=CommentForm(), user=request.user)
+    d.update(csrf(request))
+    return render_to_response("post.html", d)
+
+
 
 def archives(request):
     """
-    Archive page. Display all the posts.
+    Archive pclass CommentForm(ModelForm):
+    class Meta:
+        model = Comment
+        exclude = ["post"]age. Display all the posts.
     """
     entries = Post.objects.all().order_by('-created')
-    return render_to_response('archives.html', locals())
+    return render_to_response('archives.html', locals())
+
+
+
+
+class CommentForm(ModelForm):
+    class Meta:
+        model = Comment
+        exclude = ["post"]
+
+def add_comment(request, post_id):
+    """Add a new comment."""
+    p = request.POST
+
+    if p.has_key("content") and p["content"]:
+        author = "Anonymous"
+        if p["author"]:
+            author = p["author"]
+
+        comment = Comment(post=Post.objects.get(id=post_id))
+        cf = CommentForm(p, instance=comment)
+        cf.fields["author"].required = False
+
+        comment = cf.save(commit=False)
+        comment.author = author
+        comment.save()
+    return HttpResponseRedirect(reverse("cedricbonhomme.blog.views.post", args=[post_id]))
     # url(r'^cedricbonhomme/', include('cedricbonhomme.foo.urls')),
 
     (r'^blog/$', 'cedricbonhomme.blog.views.index'),
-    (r'^blog/(\d)/$', 'cedricbonhomme.blog.views.detail'),
+    (r'^blog/(\d)/$', 'cedricbonhomme.blog.views.post'),
     (r'^blog/archives/$', 'cedricbonhomme.blog.views.archives'),
 
+    (r"^blog/add_comment/(\d+)/$", "cedricbonhomme.blog.views.add_comment"),
     
     
     # Uncomment the admin/doc line below to enable admin documentation: