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,

January 20, 2012
Django first project and application

Now we move on to django project and application. Apa itu project ? Apa bedanya dengan application. Sesuai dengan namanya, sebuah project pada django dapat berisi 1 atau lebih applications. Misalnya kita akan membuat sistem pendukung kinerja perkantoran, di dalam sistem pendukung kinerja perkantoran terdapat aplikasi-aplikasi misalnya HRMS, kemudian General Ledger, lalu ada Payroll, SPT, Procurement system. Nah, dari gambaran tersebut, Sistem Pendukung Kinerja Perkantoran ini disebut sebagai Django Project dan aplikasi di bawahnya tersebut disebut sebagai Django Applications.

Yang pasti, untuk awalnya, kamu harus sudah menginstall Django. Terserah mau ditaruh dimana, bisa gabung di site-packages atau misalnya di /home/users/django-apps atau di htdocs (web root). Namun pemasangan django di web root sangat tidak dianjurkan, hal ini dikarenakan oleh masalah keamanan skrip dan konfigurasi kita.

Masuk ke folder instalasi django di {python_path}/Lib/site-packages/django/

Lalu untuk memulai project, :

   
python django-admin.py startproject namaproject

kemudian jika berhasil, maka akan terbentuk file sebagai berikut : __init__.py ;  manage.py ; settings.py ; url.py

Lalu untuk membuat aplikasi baru caranya :

Masuk ke folder tempat project baru kamu tadi. Saya asumsikan di /home/robee/namaproject/

kemudian ketik di terminal / command prompt untuk windows :

   
python manage.py startapp namaaplikasi

Jika berhasil, maka di bawah folder namaproject, akan terbentuk folder namaaplikasi dengan file : __init__.py ; view.py ; test.py ; model.py

Untuk menguji project kamu, ketik :

   
python manage.py runserver

dan buka browser kesayanganmu. ketik pada address bar : http://localhost:8000

Namun jika ternyata di komputer yang kamu gunakan, ada aplikasi atau service lain yang menggunakan port 8000 , atau kamu memang tidak suka dengan angka 8000 ? (heuheuheu) kamu bisa pakai angka lain (tentunya yang tidak dipakai / reserved).

   
python manage.py runserver 8888

Oke deh. Sekian dulu. Nanti saya sambung lagi.

Cheers & Selamat Mentjoba

   
nano post.html :D

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 »