Switch to date-based URLs everywhere except the feed URLs of old posts, so I don't spam everyone's feedreader.
1 """Views for the blog portion of my site"""
3 from django.core.urlresolvers import reverse
4 from django.http import HttpResponse, HttpResponseRedirect
5 from django.shortcuts import get_object_or_404
7 from agdj.blog import forms, models
8 from agdj.utils import use_template
11 def redirect_to_date_version(request, slug):
12 "Redirect requests from the old URL scheme to the new one"
13 post = get_object_or_404(models.Entry, slug=slug)
14 return HttpResponseRedirect(post.urls.view)
17 @use_template("blog/single_post.html")
18 def single_post(request, year, month, day, slug):
21 post = get_object_or_404(models.Entry, slug=slug)
22 if request.method=="POST":
23 comment_form = forms.PublicCommentForm(request.POST)
24 spam = not ("not_spam" in request.POST)
25 if comment_form.is_valid():
26 comment = comment_form.save(commit=False)
28 comment.ip = request.META['REMOTE_ADDR']
29 comment.public = not spam
31 return HttpResponseRedirect(reverse(single_post, args=[slug]))
33 comment_form = forms.PublicCommentForm()
34 comments = post.comments.filter(public=True).order_by("pub_date")
35 return dict(post=post, comments=comments, comment_form=comment_form)
38 @use_template("blog/post_list.html")
39 def post_list(request):
42 posts = models.Entry.objects.filter(public=True).order_by("-pub_date")
43 return dict(posts=posts)
46 @use_template("blog/post_list_tag.html")
47 def view_tag(request, tag):
48 "Show the entries with a tag"
49 from tagging.models import TaggedItem
51 posts = models.Entry.objects.filter(public=True).order_by("-pub_date")
52 posts = TaggedItem.objects.get_by_model(posts, tag)
53 return dict(posts=posts, tag=tag)