I Want to Build Your Website
Follow me here and let me show you what I can do!
Generate Archive List in Django
I wrote a tutorial a while ago in pdf form that walked you through creating a blog with Django. One of the pieces that I left out, was how to use the information in your blog to create a list of available months for an archive. To do this, we are going to use Djangos templatetags. Here is how we do it.
First I'm making the assumption that if your reading this, you already know what a templatetag is and where to put such a file. If not, inside your "Blog" application directory, create another directory called "templatetags". From there, create two files __init__.py and blog_archive_list.py. Open up blog_archive_list.py and put the following.
from Blog.models import Post from django.template import Library,Node
register = Library() def build_month_list(parser, token):
""" {% get_month_list %} """
return MonthListObject() class MonthListObject(Node):
def render(self, context):
context['blog_months'] = Post.objects.dates("pub_date", "month")
return '' register.tag('get_month_list', build_month_list)
That's about all there is to the templatetag, so you can close that file and save the changes. Now in your template where you put your archives, you can do something like the following:
<ul>
{% load blog_archive_list %}{% get_month_list %}
{% for month in blog_months %} <li><a href="/archives/{{ month|date:" title="{{ month|date:">{{ month|date:"F Y" }}</a></li>
{% endfor %}
</ul<
Current Projects
In Production
Recently on twitter
Categories
Archives
Content is licensed under a Creative Commons Public Domain License






Comments
No Response Yet
Why don't YOU kick it off by saying something ↓