Dec 19 2008

Builtin template tags and filters in Django

Category: Django,Pythonvbmendes @ 12:11

A good feature of Django Framework is the template tags and template filters. But it sucks when you have to load the filters in each template like this:

{% load mytemplatetags %}

It was much better if you just use the filters, without the need to load it. The reason you have to load is to achieve a better performance. But there’s a function of the template module called add_to_builtins that solves this problem. You can easily define a template tag or filter as builtin and use it like the builtin django template tags and filters. Just put this code in a file that is loaded ever, like the init.py of your project.

from django.template import add_to_builtins
add_to_builtins('path.to.templatetags.file')

Where ‘path.to.templatetags.file’ is the path of the file containing the template tags. For example, I have an app, inside my project, named mytagsapp. Inside this app I have the module templatetags with a file named mytags.py with my custom tags. So I will have to call add_builtins(‘mytagsapp.templatetags.mytags’). My project folder should look like this:

Folders structure

Folders structure

Make sure your app is in the INSTALED_APPS setting in your settings.py project. It will guarantee that the init.py of you app will get called. So just put the code above in this file (red in image).

Remember to not use this feature with all your template filters as it will mean loss of performance.

Tags: , , , ,


Dec 15 2008

Removing Empty Labels from forms.ChoiceField in ModelFormSets

Category: Django,Pythonvbmendes @ 21:52

When you use a Model Choice Field in Django, it automatically creates an empty option in the generated select widget. But in some special cases, you don’t want this empty option to be there. So I decided to find the way to remove it. It’s much more simple then you think. Just create your model declaring the model choice field like this:

fieldname = forms.ModelChoiceField(queryset=RelationModel.objects,empty_label=None)

It’s simple like that. Just set the empty_label argument to None. But whe using ModelFormSets, there is a little more work to do. You have to create a new BaseModelFormSet like this:

from django.forms.models import BaseModelFormSet</p>

<p>class MyBaseModelFormSet(BaseModelFormSet):
    def add_fields(self, form, index):
        super(MyBaseModelFormSet,self).add_fields(form,index)
        form.fields['fieldname'] = forms.ModelChoiceField(queryset=RelationModel.objects,empty_label=None)

Pay atention that you have to override the add_fields method and change your field in the form fields array. Finally you have to pass this BaseModelFormSet as argument to the modelformset_factory:

from django.forms.models import modelformset_factory</p>

<p>MyModelFormset = modelformset_factory(MyModel,formset=MyBaseModelFormSet)

Tags: , ,


Dec 07 2008

meioMask 1.0.4 version released!

Category: Javascript,jQueryfabiomcosta @ 23:51

I present you meioMask 1.0.4 – a jQuery plugin for masking inputs.

meioMask for Mootools! meioMask’s page at jquery.com meioMask’s SVN project page meioMask’s SVN

Features

  • Accepts paste event;
  • Haves fixed and reverse mask types ( allow number mask );
  • You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);
  • Supports metadata plugin;
  • Works with iPhone;
  • Allow default values;
  • Haves callbacks for invalid inputs, valid and overflow;
  • Haves function to mask strings;
  • Support for positive and negative numbers on reverse masks.

Changelog

v1.0.4

  • New mask type ‘infinite’, it allows infinite values at masks. See demos for a better understanding;
  • Added new function ‘unmaskVal’ that returns the input value without the mask. See demos for a better understanding;
  • Removed a serious bug at IE that was fixing the caret at the end of the input;

v1.0.3

  • Callback functions now receive the char number as it second parameter, so it is possible to detect exactly which key has been pressed;
  • added the signed masks. It only works with the reverse mask. See demos for more details;
  • iPhone support improved. iPhone now works 100% better than the 1.0.2 version. I’ts logic have been changed, and now it’s working with full features. :P

v1.0.2

  • added input callbacks: onInvalid, onValid and onOverflow;
  • added support for default values;
  • can now be used like $().setMask({});
  • added the function $.mask.string(string,mask) that will mask a string (see demos);
  • now the value of the input is masked at the time the mask is applyed to it.

v1.0.1

  • added support for iphone;
  • removed a bug where a fixed char would be replaced by an inputed one.

v1.0

  • initial release.

You can see the plugin page here. It contains documentation and examples. Please tell me any bug, new feature, english errors on documentation…. anything! I’ll be glad to hear your feedback and make the fixes. Hope it helps you!