Archive
As you can see, I have a weak little archive implementation up as of now. I was unfortunately led on a bit of a goose chase.
You see, my original inclination was to simply implement the archive programmatically (for lack of a better term) in the view for my root page. After a few lines of code I figured, Django has got to have a neat little automagic way to do this; just like with comments. After consulting a few different sources it seemed that generic views were the recommended solution. After creating generics for the year/month/day archives I was at an impasse. How do I get my shiny new generic views/templates "in" my main content template. Disappointingly there didn't seem to be any neat Django template trickery to do this.
I admittedly got a bit desperate and attempted to render the generic views inside of my root view, passing it as part of the context. After modifying the template a bit, this worked surprisingly well except the final product included some extra meta information that I didn't want present on the page. An example of the code as such was:
c = Context({ 'posts': posts[:15], 'categories': categories, 'archive': archive_year(request, datetime.date.today().year, Post.objects.all(), 'date', template_name='blog/post_archive_inset.html')})
As you can see I'm literally passing the generic view via context which I then insert via a template tag. I could have gotten rid of the extra meta information via slicing, but this seemed a bit too much like a hack to me. So I checked out the actual code in Django's date_based.py module and sure enough, the only bit I actually needed was the filter. The context is now changed to something like this:
c = Context({ 'posts': posts[:15], 'categories': categories, 'date_list': posts.filter(date__year=datetime.date.today().year).dates('date', 'month')})
I simply had to move the html in post_archive_inset.html to content.html where the {{ archive }} tag previously was and I immediately got the functionality I was looking for. Generics seem a great solution for when your looking for a simple page with a limited scope of information, but that wasn't what I wanted. I'd love to know if there is an accepted practice for this, assuming it differs from mine. Thus far filters seem the best way to go when you need more than minimal functionality.
Next I need to modify my new archive and detail templates so that they inherit from content.html, that way they'll default to showing the generic sidebar information. I also need to modify the categories view so it passes the new context information.
0 Comments