Source

fromlindyjam.blog.modelsimportEntry,Commentfromdjango.templateimportContext,loaderfromdjango.shortcutsimportget_object_or_404,render_to_responsefromdjango.httpimportHttpResponsefromdjango.templateimportRequestContextfrommarkdownimportmarkdownimportsimplejsonasjsonENTRIES_PER_PAGE=3COMMENT_MESSAGE="""<p class="success">Thanks for speaking up!</p>"""defindex(request,page=0):page=int(page)start_index=page*ENTRIES_PER_PAGEend_index=start_index+ENTRIES_PER_PAGEentries=Entry.objects.all().order_by('-pub_date')entries=entries.filter(published=True)[start_index:end_index]returnrender_to_response('blog/index.html',{'entries':entries,'older_page':page+1ifend_index<Entry.objects.count()elseNone,'newer_page':page-1ifpage!=0elseNone},context_instance=RequestContext(request))defentry(request,year,month,day,slug):entry=get_object_or_404(Entry,pub_date__year=year,pub_date__month=month,pub_date__day=day,slug=slug)returnrender_to_response('blog/entry.html',{'entry':entry,},context_instance=RequestContext(request))defcomment(request):errors=[]status=0fields=request.POSTiffields['name']=='':errors.append("You have to give a name!")iffields['body']=='':errors.append("You have to say something!")iflen(errors)>0:status=-1error_message='\n'.join(['<p class="error">%s</p>'%(e,)foreinerrors])returnHttpResponse(json.dumps({'status':status,'data':error_message}))entry=Entry.objects.get(pk=fields['entry_id'])new_comment=Comment(name=fields['name'],body=fields['body'],entry=entry)new_comment.save()t=loader.get_template('blog/single_comment.html')c=Context({'comment':new_comment,})returnHttpResponse(json.dumps({'status':status,'data':COMMENT_MESSAGE,'new_comment':t.render(c)}))