Permalink
Browse files

* New custom template tag

* Add stylesheet
Now finished through Chapter 6, except for some tag related templates
  • Loading branch information...
1 parent 1f4a309 commit a682b8c3c8fe58df81ab43614bf1a0c007519090 @grokcode committed Apr 1, 2012
View
@@ -0,0 +1,7 @@
+#content {
+ float: left;
+}
+
+#sidebar {
+ float: right;
+}
View
@@ -86,6 +86,7 @@
'django.contrib.flatpages',
'cms.search',
'coltrane',
+ 'coltrane.templatetags',
'tagging',
)
@@ -3,6 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>My weblog {% block title %}{% endblock %}</title>
+ <link rel='stylesheet' href='/frontend-media/style.css' type='text/css' />
</head>
<body class="{% block bodyclass %}{% endblock %}">
<div id="header"></div>
@@ -22,6 +23,28 @@ <h1 id="branding">My weblog</h1>
<h2>What is this?</h2>
{% block whatis %}
{% endblock %}
+
+ {% load coltrane_tags %}
+ <h2>Latest entries in the weblog</h2>
+ <ul>
+ {% get_latest_content coltrane.entry 5 as latest_entries %}
+ {% for entry in latest_entries %}
+ <li>
+ <a href="{{ entry.get_absolute_url }}">{{ entry.title }}</a>, posted {{ entry.pub_date|timesince }} ago.
+ </li>
+ {% endfor %}
+ </ul>
+ <h2>Latest links in the weblog</h2>
+ <ul>
+ {% get_latest_content coltrane.link 5 as latest_links %}
+ {% for link in latest_links %}
+ <li>
+ <a href="{{ link.get_absolute_url }}">{{ link.title }}</a>, posted {{ link.pub_date|timesince }} ago.
+ </li>
+ {% endfor %}
+ </ul>
+
</div>
+
</body>
</html>
View
@@ -7,9 +7,13 @@
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
+ (r'^frontend-media/(?P<path>.*)$', 'django.views.static.serve',
+ { 'document_root': '/home/smudge/work/django-practice/cms/media/' }),
(r'^tiny_mce/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root': '/home/smudge/work/django-practice/cms/scripts/tiny_mce/' }),
(r'^search/$', 'cms.search.views.search'),
+
+
(r'^weblog/categories/', include('coltrane.urls.categories')),
(r'^weblog/links/', include('coltrane.urls.links')),
(r'^weblog/tags/', include('coltrane.urls.tags')),
View
@@ -154,5 +154,5 @@ def get_absolute_url(self):
'day': self.pub_date.strftime('%d'),
'slug': self.slug })
- get_absulute_url = models.permalink(get_absolute_url)
+ get_absolute_url = models.permalink(get_absolute_url)
@@ -0,0 +1 @@
+
@@ -0,0 +1,34 @@
+from django import template
+from django.db.models import get_model
+from coltrane.models import Entry
+
+
+def do_latest_content(parser, token):
+
+ # Argument sanity checking.
+ bits = token.split_contents()
+ if len(bits) != 5:
+ raise template.TemplateSyntaxError("'get_latest_content' tag takes exactly 4 arguments")
+ model_args = bits[1].split('.')
+ if len(model_args) != 2:
+ raise template.TemplateSyntaxError("First argument to 'get_latest_content' mush be an 'application name'.'model name' string")
+ model = get_model(*model_args)
+ if model is None:
+ raise template.TemplateSyntaxError("'get_latest_content' tag got an invalid model: %s" % bits[1])
+
+ return LatestContentNode(model, bits[2], bits[4])
+
+class LatestContentNode(template.Node):
+
+ def __init__(self, model, num, varname):
+ self.model = model
+ self.num = int(num)
+ self.varname = varname
+
+ # Puts the 5 latest entries in the context
+ def render(self, context):
+ context[self.varname] = self.model._default_manager.all()[:self.num]
+ return ''
+
+register = template.Library()
+register.tag('get_latest_content', do_latest_content)

0 comments on commit a682b8c

Please sign in to comment.