Skip to content

Commit

Permalink
Added custom templatetag get_latest_blog_entry for django_website
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/djangoproject.com@33 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrian committed Jul 14, 2005
1 parent 1fe4d38 commit d7b7730
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions django_website/apps/blog/models/blog.py
Expand Up @@ -11,6 +11,7 @@ class Entry(meta.Model):
meta.CharField('author', 'author', maxlength=100), meta.CharField('author', 'author', maxlength=100),
) )
ordering = (('pub_date', 'DESC'),) ordering = (('pub_date', 'DESC'),)
get_latest_by = 'pub_date'
admin = meta.Admin( admin = meta.Admin(
fields = ( fields = (
(None, {'fields': ('pub_date', 'slug', 'author', 'headline', 'body')}), (None, {'fields': ('pub_date', 'slug', 'author', 'headline', 'body')}),
Expand Down
Empty file.
27 changes: 27 additions & 0 deletions django_website/apps/blog/templatetags/latestblogentry.py
@@ -0,0 +1,27 @@
from django.core import template
from django.models.blog import entries

class LatestBlogEntryNode(template.Node):
def __init__(self, varname):
self.varname = varname

def render(self, context):
try:
e = entries.get_latest()
except entries.EntryDoesNotExist:
e = None
context[self.varname] = e
return ''

def do_get_latest_blog_entry(parser, token):
"""
{% get_latest_blog_entry as latest_entry %}
"""
bits = token.contents.split()
if len(bits) != 3:
raise template.TemplateSyntaxError, "'%s' tag takes two arguments" % bits[0]
if bits[1] != 'as':
raise template.TemplateSyntaxError, "First argument to '%s' tag must be 'as'" % bits[0]
return LatestBlogEntryNode(bits[2])

template.register_tag('get_latest_blog_entry', do_get_latest_blog_entry)

0 comments on commit d7b7730

Please sign in to comment.