Posted by andu
Sun, 2007-08-05 13:28
Later edit: there's already a tag (and I should really check better next time) which does this. What this tag does extra, however, is allow you to check for the remainder as well.
In one of the projects at work I needed to compare the result of a modulo operation to another value in the template. Since the default Django tag library does not offer this kind of thing, I decided to write it.
Why would you need this kind of functionality? In my case I needed to output a list of items and put some markup after each 5 of them. I could have solved it by doing some magic in the views code but I did not want to mix the template logic with the views. And since I never wrote a template tag for Django, it looked like a nice opportunity.
You would use it like this:
{% ifmodulo forloop.counter 4 0 %}
<!-- do something -->
{% else %}
<!-- do something else -->
{% endifmodulo %}
{% ifnotmodulo 5 3 %}
<!-- do something -->
{% else %}
<!-- do something else -->
{% endifmodulo %}
Here's the code and you can also download the file by using the link at the bottom of this post:
Posted by andu
Tue, 2006-03-07 10:52
One of the problems I faced when using Django's admin was the filter part overlapped the table with data, when I showed lots of fields in the admin listing.
Martin demanded a fix, so I did a quick hack today and made it toggable ( ie you can toggle up and toggle down the filter pane). If you want to aply a filter, show it. If not, leave it there and browse the listings.
I know it could have been done with just some simple javascript that manipulates the display css property, thus avoiding loading 3 js files, but I liked the effect. :) If anyone wants a slimmed version, let me know and I'll post one.
Here's what you have to do:
- Download moo.fx.js, moo.fx.pack.js and prototype.lite.js from moo.fx
- Put them somewhere in the head section of admin/base.html template. In my case:
<script type="text/javascript" src="/static/js/admin/prototype.lite.js">
</script>
<script type="text/javascript" src="/static/js/admin/moo.fx.js">
</script>
<script type="text/javascript" src="/static/js/admin/moo.fx.pack.js">
</script>
- Add the following code to the head, the same file:
<script type="text/javascript">
var filtruEff;
function addEvent(object, type, handler)
{
if (object.addEventListener) {
object.addEventListener(type, handler, false);
} else if (object.attachEvent) {
object.attachEvent(['on',type].join(''),handler);
} else {
object[['on',type].join('')] = handler;
}
}
addEvent(window,"load",filtruHandler);
function filtruHandler() {
var x = document.getElementById("filtruDiv");
if (!x)
return;
filtruEff = new fx.Combo('filtruDiv', {opacity:true, duration:600});
filtruEff.hide();
}
</script>
-
Modify the admin/filters.html template to look like this:
{% load admin_list %}
{% if cl.has_filters %}<div id="changelist-filter">
<h2>Filter (<a href="javascript:filtruEff.toggle();">Toggle</a>)</h2>
<div id="filtruDiv">
{% for spec in cl.filter_specs %}
{% filter cl spec %}
{% endfor %}</div>
</div>
{% endif %}
Posted by andu
Mon, 2006-02-13 10:08
Don't use the post url in a form like "/contact" because you will lose the post data. The correct way is "/contact/".
The reason this doesn't work is that when passed "/contact" you get a redirection to "/contact/", and all the post data is lost. For get, it works though.
Posted by andu
Wed, 2006-02-01 10:37
Am inceput sa studiez Django. Dupa primele ore petrecute in compania lui, imi place foarte mult.
Voi posta aici diversele mele experiente cu el. Pentru inceput, instalarea sub un Debian si configurarea unei aplicatii.