Thursday, January 31, 2013

Adding css class to a django form field in a template

One way of adding a css class to a Django form field in template is to create a widget for each of the form fields. But It is often annoying to define each field in the ModelForm again just to use widget. There is another way of doing this without creating widgets. All you need to do is to use an app called "widget_tweaks". You can install it using this command:
pip install django-widget-tweaks
Then add 'widget_tweaks' to INSTALLED_APPS of your Django project. To use it in your template, load the app in your template first:
{% load widget_tweaks %}
To add a css class to a form field, use the 'add_class' template filter.
{{ form.title|add_class:"css_class_1" }}
Multiple css classes can also be added.
{{ form.title|add_class:"css_class_1 css_class_2 css_class_3" }}
This app also provides several template filters that can alter CSS classes and HTML attributes of Django form fields. Details can be found here.

Saturday, December 29, 2012

How to detect iPhone, iPod or iPad using javascript

Most of the sites have a mobile version now a days. In fact they have multiple mobile versions of their site as screen size varies between phone and tablet. To have a mobile version for your site, you need to detect the client first. Detecting client can be done either on the server side (using your preferred scripting language) or the client side. I am showing here how you can detect it on the client side using Javascript. After detecting the client type, you can render an appropriate version of the site. Even you can use this technique to make your jQuery plugin mobile compatible.
var client;

if( navigator.userAgent.match(/iPhone/i) )
{
   client = 'iPhone';   
}
else if( navigator.userAgent.match(/iPod/i) )
{
   client = 'iPod';   
}
else if( navigator.userAgent.match(/iPad/i) ) 
{
   client = 'iPad';   
}

Friday, December 21, 2012

How to prevent back button from showing data after logout

In a secure web application where user has to login to see contents, it is not desirable that previous page(s) can be seen using browser's back button after a user has logged out. This can happen if you allow caching for your secure pages. The solution is easy. You need to set the cache-control and expiration date headers for your secured pages. The expiration date should be set to a past date so that it expires immediately. Here is an example:
Cache-Control: no-cache
Expires: Fri, 31 Dec 1990 12:00:00 GMT

How to install ruby 1.9 on CentOS 5

Recently I had to install ruby 1.9.1 on a CentOS 5 machine. I had a hard time in finding the right commands to do this simple task. So I am sharing the steps here:
cd /usr/local/src
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p376.tar.gz
tar xzvf ruby-1.9.1-p376.tar.gz
cd ruby-1.9.1-p376
./configure && make
sudo make install

Tuesday, December 11, 2012

phpMyAdmin automatic logout problem: No activity within 1440 seconds

One of the very few annoying features of phpmyadmin is the automatic logout after inactive 1440 seconds. I guess all users of phpmyadmin has faced this problem. The remedy is pretty easy. First go to "phpmyadmin" directory. Open the file named "config.inc.php" and add the following line:
$cfg['LoginCookieValidity'] = 3600 * 24; //24 hours
This will make the default time of 1440 seconds to 24 hours which is good enough I guess. Of course you can use any amount of time you want.

Friday, June 3, 2011

Cookie settings problem in IE for Facebook iFrame Applications

Recently I was working on a iFrame Facebook application where I faced a problem with cookie in Internet Explorer. I was using cookie to make authenticated API calls from server. This was working fine for all browsers except IE. The cookie was not working with the default cookie settings of IE. As the cookie was getting set in an iframe, IE view it as a third-party cookie and rejects it as the default cookie setting for IE does not allow third-party cookies. The solution for this problem was to have a compact privacy policy on the pages where cookies are needed. Having a compact privacy policy is easy. You need set a compact P3P header on the required pages. I needed to set the following PHP header to solve my problem:
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');

Changing parent window's url from iframe

Sometimes it is needed to change the parent window's url from iframe. It is pretty easy to do with javascript. You need to execute the following javascript from the iframe whenever you want to change parent window's url:
parent.location = "YOUR REDIRECT URL";
That's it.

Monday, May 16, 2011

Transparent area goes black after copying/cropping PNG image using PHP GD library

Recently I was working on a project in which I needed to do some image manipulations using PHP GD library. Specifically I needed to crop some areas from PNG images. I was able to crop the images pretty easily, but there was a problem in the resulting images: the transparent areas were going black which was making the resulting images unusable. To solve this problem I had to do the following:

$cropped_image = ImageCreateTrueColor($crop_width, $crop_height);
imagealphablending( $cropped_image, false );
imagesavealpha( $cropped_image, true );
$transparent = imagecolorallocatealpha($cropped_image, 0, 0, 0, 127);
imagefill($cropped_image, 0, 0, $transparent);
$original_image = ImageCreateFromPNG($image_to_crop);
imagealphablending( $original_image, false );
imagesavealpha( $original_image, true );
ImageCopy($cropped_image, $original_image, 0, 0, $left, $top, $width, $height);
ImagePNG($cropped_image, $save_path);
Here $image is the cropped image. $width and $height are the width and height of the original image respectively. $left and $top are the left and top location of the rectangular portion of the original image which should be cropped. And $save_path is the location where the cropped image should be saved.

Hope this helps someone.

Monday, January 24, 2011

Reset auto_increment value of a MySQL database table after deleting several rows

In a MySQL table with a lot of data, it is very common that last few rows will get deleted frequently. If any column has auto_increment, this will result in a gap in the generated column values. It is very easy to reset the auto_increment value to the correct value which will not result in any gap between the column values. Of course, it is only possible to prevent the gap if the deleted rows were the last few rows of the table. Here is the sql that will reset auto_increment:
ALTER TABLE table_name_to_reset AUTO_INCREMENT=1
That's it. Hope this helps.

Sunday, November 28, 2010

Time to say goodbye to FBML

It seems like it's time to say goodbye to FBML. Facebook will stop allowing to create new FBML applications soon. They will also allow iframe in profile tabs. They are encouraging iframe for both canvas applications and profile tabs. Check this link for more: http://developers.facebook.com/blog/post/415