I tried to think of a way to use generic views to show all my blog posts on one page organized chronologically with each post title under one month name and all the month names under one year name.
Like so:
`
2009
October
My last example blog entry
Entry even before the last one
First of October
September
Only one entry in Sept.
2008
December
It is cold in December
First posting of my blog
`
I could not think of a way to do this using generic views, before I wrote a view and some template logic that does.
The view code is here:
[http://www.djangosnippets.org/snippets/1766/](http://www.djangosnippets.org/snippets/1766/)
Any suggestions for design patterns are greatly appreciated.
You have to put this code in your *searchengines* Firefox folder in a file called **djangosnippets.xml**
After restarting Firefox, you will be able to seek DjangoSnippets very easily.
I also create a favicon.ico since djangosnippets.org didn't have one ...
Path example : /usr/lib/iceweasel/searchplugins/djangosnippets.xml
Active class for navigation link
{% navclass default current url %}
First argument is the default class and second is the current class
Third argument is the url name(s) of the page
example: <a class="{% navclass leftnav curentnav name1,name2 %}" href="/about/">
By default all forms created using inlineformset_factory are displayed as tables (because there is only a .as_table method) and there are no .as_p or .as_ul methods in them, so you need to do that by hand.
Template tag to obfuscate emails and other spam sensitive information.
Usage
obfuscate '[email protected]' 'Link display' 'Link title'
obfuscate '[email protected]' 'Link display'
obfuscate 'phone number'
Renders complex xhmtl compliant javascript.
May need caching as it renders a new code every time.
The proper looking email links rendered to the browser are not in the source code that way, and are rendered by javascript, so robots can't extract it. Its a trick way to have the email displayed properly without getting spamed as a result.
Unless the robots are getting smarter, this has worked for me thus far.
Variation on dictsort using attribute access. Nested attributes can be used, like, "obj.attr.attr_attr"
Example usage:
{% for entry in entries|sortby:'category.title' %}
Based on [1609](http://www.djangosnippets.org/snippets/1609/)
entity encoded string for a somewhat safer email-address.
this filter encodes strings to numeric entities, almost every standard-browsers decodes the entities and display them the right way.
needless to say that bots are smart, so this is not a 100% guaranteed spam prevention.
As there is no straight way to re-produce the real tabular inline formsets you get in django-admin, here is how this template has to look like if you do it form your own formsets generated from formset factories.
Easy to use range filter.
Just in case you have to use a "clean" for loop in the template.
Inspired by [Template range tag](http://www.djangosnippets.org/snippets/779/)
Copy the file to your templatetags and load them.
[Django doc | Custom template tags and filters](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/)
Django tagging by default doesn't provide a templatetag to get the related objects for another object. Even though this is implemented as a model. Still, one can use the existing templatetags to achieve the same outcome.
Of course, writing a custom templatetag would be more efficient in terms of database access.