Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added custom templatetag get_latest_blog_entry for django_website

git-svn-id: http://code.djangoproject.com/svn/djangoproject.com@33 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d7b773015fa89d8f51e3a5c3c7df2f76b0a2e326 1 parent 1fe4d38
adrian authored
View
1  django_website/apps/blog/models/blog.py
@@ -11,6 +11,7 @@ class Entry(meta.Model):
meta.CharField('author', 'author', maxlength=100),
)
ordering = (('pub_date', 'DESC'),)
+ get_latest_by = 'pub_date'
admin = meta.Admin(
fields = (
(None, {'fields': ('pub_date', 'slug', 'author', 'headline', 'body')}),
View
0  django_website/apps/blog/templatetags/__init__.py
No changes.
View
27 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)
Please sign in to comment.
Something went wrong with that request. Please try again.