<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>ajax/mywebsite/statics/javascript/ajax_comments.js</filename>
    </added>
    <added>
      <filename>ajax/mywebsite/statics/javascript/jquery-1.3.2.min.js</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,10 @@
 {% extends &quot;base.html&quot; %}
 
+{% block head %}
+    {{ super }}
+    &lt;script type=&quot;text/javascript&quot; src=&quot;/statics/javascript/ajax_comments.js&quot;&gt;&lt;/script&gt;    
+{% endblock %}
+
 {% load entrytag commenttag formtag %}
 
 {% block title %}{{ super }} - entry: {{ entry.title }}{% endblock %}
@@ -7,11 +12,12 @@
 {% block body %}
     {% render_entry entry 'show' %}
 
-    {% for comment in entry.comments %}
-        {% render_comment comment %}
-    {% endfor %}
+    &lt;div id=&quot;comments-box&quot;&gt;
+        {% for comment in entry.comments %}
+            {% render_comment comment %}
+        {% endfor %}
+        &lt;div id=&quot;comment-preview&quot;&gt;&lt;/div&gt;
+    &lt;/div&gt;
     
-    {% if form %}
-        {% render_form form %}
-    {% endif %}
+    &lt;div id=&quot;comment-form&quot;&gt;&lt;/div&gt;
 {% endblock %}</diff>
      <filename>ajax/mywebsite/blog/templates/blog/entry.html</filename>
    </modified>
    <modified>
      <diff>@@ -2,6 +2,8 @@ from django.conf.urls.defaults import patterns
 
 
 urlpatterns = patterns('blog.views',
-        (r'^$',                     'entries_list'),
-        (r'^(?P&lt;slug&gt;.+)/$',        'show_entry'),
+        (r'^$',                          'entries_list'),
+        (r'^(?P&lt;slug&gt;[\w-]+)/form/$',    'get_comment_form'),
+        (r'^(?P&lt;slug&gt;[\w-]+)/preview/$', 'comment_preview'),
+        (r'^(?P&lt;slug&gt;[\w-]+)/$',         'show_entry'),
 )</diff>
      <filename>ajax/mywebsite/blog/urls.py</filename>
    </modified>
    <modified>
      <diff>@@ -5,7 +5,10 @@ from django.shortcuts import render_to_response, get_object_or_404
 
 from models import Entry, Comment
 from forms import CommentForm
+from templatetags.commenttag import render_comment
+from templatetags.formtag import render_form
 
+from utils.ajax import json_response
 
 def entries_list(request):
     context = {}
@@ -22,19 +25,43 @@ def show_entry(request, slug):
     context = {}
     entry = get_object_or_404(Entry, slug=slug)
     if request.POST:
-        comment = Comment(entry=entry)
-        form = CommentForm(request.POST, instance=comment)
-        if form.is_valid():
-            request.session['comment_form_data'] = {
-                    'user': request.POST['user'],
-                    'mail': request.POST['mail'],
-                }
-            request.session.modified = True
-            comment = form.save()
-            form = None
-    else:
-        form_data = request.session.get('comment_form_data', {})
-        form = CommentForm(initial=form_data)
-    context['form'] = form
+        return post_comment(request, entry)
     context['entry'] = entry
     return render_to_response('blog/entry.html', context)
+
+@json_response(only_ajax=True)
+def get_comment_form(request, slug):
+    context = {}
+    form_data = request.session.get('comment_form_data', {})
+    form = CommentForm(initial=form_data)
+    context['form'] = render_form(form)
+    context['status'] = 'ok'
+    return context
+
+@json_response(only_ajax=True)
+def post_comment(request, entry):
+    context = {}
+    comment = Comment(entry=entry)
+    form = CommentForm(request.POST, instance=comment)
+    if form.is_valid():
+        request.session['comment_form_data'] = {
+                'user': request.POST['user'],
+                'mail': request.POST['mail'],
+            }
+        request.session.modified = True
+        comment = form.save()
+        context['status'] = 'ok'
+        context['comment'] = render_comment(comment)
+    else:
+        context['status'] = 'fail'
+        context['form'] = render_form(form)
+    return context
+
+@json_response(only_ajax=True)
+def comment_preview(request, slug):
+    context = {}
+    p = request.POST.copy()
+    comment = Comment(user=p['user'], text=p['text'], mail=p['mail'])
+    context['comment'] = render_comment(comment)
+    context['status'] = 'ok'
+    return context</diff>
      <filename>ajax/mywebsite/blog/views.py</filename>
    </modified>
    <modified>
      <diff>@@ -3,8 +3,9 @@
 &lt;html&gt;
 &lt;head&gt;
     &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
-    {% block head %}{% endblock %}
     &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/statics/css/main.css&quot;&gt;
+    &lt;script type=&quot;text/javascript&quot; src=&quot;/statics/javascript/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;    
+    {% block head %}{% endblock %}
     &lt;title&gt;Mywebsite{% block title %}{% endblock %}&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;   </diff>
      <filename>ajax/mywebsite/templates/base.html</filename>
    </modified>
    <modified>
      <diff>@@ -1,3 +1,22 @@
 # -*- coding: utf-8
 
+from functools import wraps
+
+import json
+
+from django.http import Http404
+from django.http import HttpResponse
+
+
+def json_response(only_ajax):
+    def decorator(fun):
+        @wraps(fun)
+        def view(request, *args, **kwds):
+            if only_ajax and not request.is_ajax:
+                raise Http404
+            response = fun(request, *args, **kwds)
+            response = json.dumps(response)
+            return HttpResponse(response, mimetype='application/javascript')
+        return view
+    return decorator
 </diff>
      <filename>ajax/mywebsite/utils/ajax.py</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>5862e2a0144d568b4b918e486fbba58af0dff7ba</id>
    </parent>
  </parents>
  <author>
    <name>Piotr Husiaty&#324;ski</name>
    <login></login>
    <email>phusiatynski@gmail.com</email>
  </author>
  <url>http://github.com/husio/tutorials/commit/bbcba414e706fd7a135377ec00c3d15a90808eaf</url>
  <id>bbcba414e706fd7a135377ec00c3d15a90808eaf</id>
  <committed-date>2009-08-17T13:03:22-07:00</committed-date>
  <authored-date>2009-08-17T13:03:22-07:00</authored-date>
  <message>django blog w wersji z ajax.</message>
  <tree>83b805f51ac9faa57a5ad18b03072c6e8105f6ea</tree>
  <committer>
    <name>Piotr Husiaty&#324;ski</name>
    <login></login>
    <email>phusiatynski@gmail.com</email>
  </committer>
</commit>
