legacy-dotfiles/blog/views.py
2011-10-17 01:34:04 +02:00

55 lines
1.4 KiB
Python

# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import Context, loader
from django.views.generic.simple import direct_to_template
from blog.models import Post, Tag
def index(request, page=0):
item_count = 7
page = int(page)
start_num = (page * item_count)
end_num = start_num + item_count
post_list = Post.objects.all().order_by("-postdate")[start_num:end_num]
has_previous = page > 0
has_next = end_num < Post.objects.all().count()
c = {
'postlist': post_list,
'has_next': has_next,
'has_previous': has_previous,
'next_page': page + 1,
'previous_page': page - 1
}
return direct_to_template(request, "blog/posts.html", c)
def post(request, post_id):
post = Post.objects.filter(pk=post_id)
t = loader.get_template("blog/posts.html")
c = Context({
'postlist': post,
})
return HttpResponse(t.render(c))
def tags(request):
tag_list = Tag.objects.all()
t = loader.get_template("blog/tags.html")
c = Context({
'taglist': tag_list,
})
return HttpResponse(t.render(c))
def tag(request, tag_name):
post_list = Post.objects.filter(tags__name=tag_name)
t = loader.get_template("blog/posts.html")
c = Context({
'postlist': post_list,
})
return HttpResponse(t.render(c))