4👍
I’ll start.
http://www.djangosnippets.org/snippets/1350/
Smart {% if %} template tag
If you’ve ever found yourself needing more than a test for True, this tag is for you. It supports equality, greater than, and less than operators.
Simple Example
{% block list-products %}
{% if products|length > 12 %}
<!-- Code for pagination -->
{% endif %}
<!-- Code for displaying 12 products on the page -->
{% endblock %}
3👍
smart-if. Allows normal if x > y
constructs in templates, among other things.
A better if
tag is now part of Django 1.2 (see the release notes), which is scheduled for release on March 9th 2010.
- [Django]-Unicode string display on Django template
- [Django]-Save memory in Python. How to iterate over the lines and save them efficiently with a 2million line file?
- [Django]-Where to you monkey patch the Django user model?
- [Django]-Django Count lines in a Subquery
1👍
James Bennet’s over-the-top-dynamic get_latest
tag
edit as response to jpartogi’s comment
class GetItemsNode(Node):
def __init__(self, model, num, by, varname):
self.num, self.varname = num, varname
self.model = get_model(*model.split('.'))
self.by = by
def render(self, context):
if hasattr(self.model, 'publicmgr') and not context['user'].is_authenticated():
context[self.varname] = self.model.publicmgr.all().order_by(self.by)[:self.num]
else:
context[self.varname] = self.model._default_manager.all().order_by(self.by)[:self.num]
return ''
<div id="news_portlet" class="portlet">
{% get_sorted_items cms.news 5 by -created_on as items %}
{% include 'snippets/dl.html' %}
</div>
<div id="event_portlet" class="portlet">
{% get_sorted_items cms.event 5 by date as items %}
{% include 'snippets/dl.html' %}
</div>
I call it get_sorted_items
, but it is based on James’ blog-post
- [Django]-Adding new form fields dynamically in admin
- [Django]-SSL error while running syncdb on Django with PostgreSQL
- [Django]-How can I resolve this linting error? "Consider adding meta keywords. <html lang="en-US"> (H031)"
- [Django]-Django tables 2: Hyperlinking items in a column
- [Django]-How to have nested url namespaces with a dynamic first part in Django
1👍
In come case {% autopaginate queryset %} (http://code.google.com/p/django-pagination/) is useful. For example:
#views.py
obj_list = News.objects.filter(status=News.PUBLISHED)
# do not use len(obj_list) - it's evaluate QuerySet
obj_count = obj_list.count()
#news_index.html
{% load pagination_tags %}
...
# do not use {% if obj_list %}
{% if obj_count %}
<div class="news">
<ul>
{% autopaginate obj_list 10 %}
{% for item in obj_list %}
<li><a href="...">{{ item.title }}</a></li>
{% endfor %}
</ul>
</div>
{% paginate %}
{% else %}
Empty list
{% endif %}
Note, that obj_list must be lazy – read http://docs.djangoproject.com/en/dev/ref/models/querysets/#id1
- [Django]-NoReverseMatch at /, u'opts|admin_urlname' is not a registered namespace Django 1.4.1
- [Django]-How to call external python script in django from a dropdown button on click event in the html
- [Django]-Deep JSON Serialization of Django objects
- [Django]-DJANGO: Change field size of ModelMultipleChoiceField?