April 10, 2012
MySQL Sock Path Problem In MySQLdb Python and Django

Yesterday, I posted an article about the way to installing and configuring mod wsgi. After application hooked up successfully, I faced new problem. Because I use MySQL in XAMPP for Linux package, the MySQLdb for python didn’t find mysql sock on the default path. So, you need to manually specify the unix socket.

If you run the connection manually, probably the example is just like this :

import MySQLdb
db = MySQLdb.connect( host = 'localhost', user = 'root', passwd = '', db = 'djangodb')

Then I got error like this :

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/pymodules/python2.7/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 188, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

So that means I need to specify unix socket. The syntax will be like this :

db = MySQLdb.connect( host = 'localhost', user = 'root', passwd = '', db = 'djangodb', unix_socket = '/opt/lampp/var/mysql/mysql.sock')

How I found mysql sock file path ? Just type this at terminal. And I got the path

ps -aux | grep mysqld
nobody    6959  0.0  1.3 187900 27788 pts/4    Sl   01:41   0:14 /opt/lampp/sbin/mysqld --basedir=/opt/lampp --datadir=/opt/lampp/var/mysql --plugin-dir=/opt/lampp/lib/mysql/plugin --user=nobody --log-error=/opt/lampp/var/mysql/work085.err --pid-file=/opt/lampp/var/mysql/work085.pid --socket=/opt/lampp/var/mysql/mysql.sock --port=3306
root     11925  0.0  0.0   4448   796 pts/4    S+   16:04   0:00 grep --color=auto mysqld

So, I need to change django settings too. I just put socket path (/opt/lampp/var/mysql/mysql.sock) as database host. Seems ridiculous but it works properly.

Cheers,

Source :

http://passingcuriosity.com/2009/specifying-a-unix-socket-using-mysql-with-django/

https://code.djangoproject.com/ticket/1481

April 10, 2012
Mod WSGI Installation on Ubuntu 11.10

It’s been so long time I didn’t yet post anything. Office movement to new place was so hectic and take much time and energy also. In this post I would share my experience in Installing and configuring Mod WSGI which is needed if you in web development using Apache as your web server and you write web apps in Python based or using Django (or else Python Web Frameworks). In this occasion, I use XAMPP for Linux instead of dependent Apache and MySQL. If you use XAMPP for Linux, you also need the developer version. Because later on, you will compile mod_wsgi using apxs and python.

First, download XAMPP for Linux both XAMPP and XAMPP Development package.

Second, after both files downloaded. Extract it to /opt

tar xvfz xampp-linux-1.7.7.tar.gz -C /opt
tar xvfz xampp-devel-1.7.7.tar.gz -C /opt

Third, download latest mod_wsgi from this server.

Fourth, extract mod_wsgi

tar xvfz mod_wsgi.X.Y.tar.gz

Fifth, Install python-dev

sudo apt-get install python-dev

Sixth, compile mod_wsgi source code

./configure --with-apxs=/opt/lampp/bin/apxs --with-python=/usr/bin/python
make
make install

mod_wsgi file will be created on /opt/lampp/modules/mod_wsgi.so

You can enable it by load it on httpd.conf just like another module.

Cheers,

February 27, 2012
How to Round Floating Number in Javascript

Rounding a floating number is easy as pie. There are many ways to do that. Let’s see the example.

Using .toFixed

// toFixed(3) 
var num = 58.8963;
var result = num.toFixed(3); // result equal to 58.896

// toFixed(2) when the number has no decimal places
num = 70;
result = num.toFixed(3); // result equal to 70.000

Error in Floating Point :

num = 162.295
num *= 100 // 16229.499999999998
num = Math.round(num) // 16229
num /= 100 // 162.29

As we see at second point, num will return exact value. So beware to use it for calculation.

(Source: http)

January 26, 2012
Mengambil Nilai dari URI Parameter

Sekedar ingin berbagi tips, bagaimana cara mengambil sebuah nilai / value yang sebelumnya di parsing pada URI parameter. Misalkan  dalam sebuah URI : http://localhost/myapp?id=88&param1=robee , nah, kita mau mengambil variable param1 yang berisi nilai robee tersebut untuk diolah lagi.

Buat function :

   
var QueryString = function () {
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
} ();

Untuk Menggunakannya :

   
nama= QueryString.param1;

January 13, 2012
Mess up with Django

Postingan pertama di tahun 2012 ini, saya cuma ingin mendokumentasikan bagaimana cara menggunakan Django (baca : jenggo) pada sistem operasi Microsoft Windows XP. Dan sebenarnya ini juga berlaku untuk sistem operasi Microsoft Windows 7.

Sekedar informasi, Django merupakan Python Web Framework yang membantu web development berbasis Python dengan cepat. Yang pasti seperti Framework PHP, Django juga menggunakan prinsip MVC pada dasarnya, namun Django menganut prinsip DRY, yang akan membuat code lebih clean, smooth dan rigid.

Pada dasarnya, Django diperuntukkan pengembang web yang mengarah ke online newspaper atau online magazine. Namun pada kenyataannya, seperti Framework PHP (CodeIgniter dsb), Django sering juga dipakai untuk keperluan backend system.

Read More

Liked posts on Tumblr: More liked posts »