django

If modulo template tag for Django

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:

  1. {% ifmodulo forloop.counter 4 0 %}
  2. <!-- do something -->
  3. {% else %}
  4. <!-- do something else -->
  5. {% endifmodulo %}

  1. {% ifnotmodulo 5 3 %}
  2. <!-- do something -->
  3. {% else %}
  4. <!-- do something else -->
  5. {% endifmodulo %}

Here's the code and you can also download the file by using the link at the bottom of this post:

Django filter hack

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 %}
    

Today's Django tip

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.

Is it done yet? what about now? Faster development frameworks

Posted by andu
Wed, 2006-02-01 23:38

Via Martin:

Alex and I have been talking a long time (since May of last year) about doing some "cool stuff" for the Romanian web but because of other commitments, the time just seems to have drifted away...but no longer...decisions have to made, action taken or heads will roll...we've chosen a development framework!

The requirements
~ rapid application development both for work and play
~ build stuff based on data structures but assume that the data structure and business logic will change (it always does)
~ if the data or logic changes, rebuild from scratch if you have to
~ put all the clever stuff into modules separate from the logic and the data that you can just plug in to the new data structure...
~ there should exist both documentation and a large community which actually responds both to newbie questions and esoteric or advanced problems
~ depth of knowledge pool in Romania
~ cool ajaxy plugins would be nice but not at the expense of the above

How we evaluated
~ we read blogs and other reviews on the internet and believed everything we saw - well - it's on the web so must be true...
~ we gave extra points to frameworks with cool videos of full enterprise applications being developed in a lunchbreak
~ the number of lines of code is really important (apparently)
~ if we really really thought it was important to actually try the framework to get a feel of its capabilities then obviously the videos/screencasts were not cool enough so it lost points...

The caveats
~ This is not a technical review...because I would sound oh so stupid if I tried... I won't go into CRUD, MVC (Model View Controller) frameworks, scaffolding, agile development or all the other technical terms - there are brighter people than me who have explained it better...
~ this is not an exaustive list
~ most of this is from memory so I have probably confused a couple of the frameworks - my apologies to all concerned - I will fill in later with links to comments/reviews I bookmarked at the time)

Django

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.