All pastes #2131623 Raw Edit

kamil

public text v1 · immutable
#2131623 ·published 2012-03-23 11:23 UTC
rendered paste body
# Create your views here.from django.core.paginator import Paginator, InvalidPage, EmptyPagefrom django.shortcuts import render_to_response, get_object_or_404from django.http import HttpResponse, Http404, HttpResponseRedirectfrom django.core.urlresolvers import reversefrom django.core.context_processors import csrffrom blog.models import *from captcha.fields import CaptchaFieldfrom django.forms import ModelForm, Formdef main(request):    """Main listing."""    posts = Post.objects.all().order_by("-created")    paginator = Paginator(posts, 2)    try: page = int(request.GET.get("page", '1'))    except ValueError: page = 1    try:        posts = paginator.page(page)    except (InvalidPage, EmptyPage):        posts = paginator.page(paginator.num_pages)    return render_to_response("blog/list.html", dict(posts=posts, user=request.user))def post(request, pk):    """Single post with comments and a comment form."""    post = Post.objects.get(pk=int(pk))    d = dict(post=post, user=request.user)    return render_to_response("blog/post.html", d)class CommentForm(ModelForm):    class Meta:        model = Comment        exclude = ["post"]class CaptchaTestForm(Form):    captcha = CaptchaField()    class Meta:        model = Comment        exclude = ["post"]    def add_comment(request, pk):    """Add a new comment."""    if request.POST:        form = CaptchaTestForm(request.POST)        if form.is_valid():            human = True    else:        form = CaptchaTestForm()#    if homan == True:#        CaptchaTestModelForm    p = request.POST        if p.has_key("body") and p["body"]:        author = "Anonymous"        if p["author"]:            author = p["author"]        comment = Comment(post=Post.objects.get(pk=pk))        cf = CommentForm(p, instance=comment)        cf.fields["author"].required = False        comment = cf.save(commit=False)        comment.author = author        comment.save()    return HttpResponseRedirect(reverse("blog.views.post", args=[pk]))def post(request, pk):    """Single post with comments and a comment form."""    post = Post.objects.get(pk=int(pk))    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("blog/post.html", d)