Blogs with tag [Django]
run Django ใน mode prefork นี่น่ากลัวแฮะ
น่ากลัวตอนมัน fork process แล้วสั่ง ps ดู เห็น process เยอะจัง แต่เค้าว่ากันว่ามันจะเร็วกว่า threaded แต่ถ้าใช้บน windows ก็ต้องใช้ threaded อย่างเดียว
cwt@www:~/cwt$ ps x
PID TTY STAT TIME COMMAND
4567 ? S 0:00 sshd: cwt@pts/0
4568 pts/0 Ss 0:00 -bash
4770 ? S 0:00 /usr/bin/python -O manage.py runfcgi host=127.0.0.1 port=9114 method=prefork pidfile=/home/cwt/django.pid
4771 ? S 0:00 /usr/bin/python -O manage.py runfcgi host=127.0.0.1 port=9114 method=prefork pidfile=/home/cwt/django.pid
4772 ? S 0:00 /usr/bin/python -O manage.py runfcgi host=127.0.0.1 port=9114 method=prefork pidfile=/home/cwt/django.pid
4773 ? S 0:00 /usr/bin/python -O manage.py runfcgi host=127.0.0.1 port=9114 method=prefork pidfile=/home/cwt/django.pid
4774 ? S 0:00 /usr/bin/python -O manage.py runfcgi host=127.0.0.1 port=9114 method=prefork pidfile=/home/cwt/django.pid
4775 ? S 0:00 /usr/bin/python -O manage.py runfcgi host=127.0.0.1 port=9114 method=prefork pidfile=/home/cwt/django.pid
4784 pts/0 R+ 0:00 ps x
อักษรพิเศษใน Title
ตัวอักษร #;/?:@&=+$, มันเป็นอันตรายต่อ URL เลยต้องกำจัดมันออกไปก่อนจะ save ลง database
def save(self):
invalid_chars = '#;/?:@&=+$,'
safe_title = ''
for char in self.title.strip():
if char not in invalid_chars:
safe_title += char
self.title = safe_title
super(Blog, self).save()
Thai locale in Django-SVN
Just updated many things in Bashell.COM to the newer version including Django which I always update to the latest svn. While the svn shown its progress, I saw 'th' in the locale directory. So after finish, I tried to changed my settings.py from
LANGUAGE_CODE = 'en-us'
to
LANGUAGE_CODE = 'th-th'
Then I go to the admin page and saw this:

and this:

Thanks to everybody who make this Thai locale!
การใช้ Nginx กับ Django
(กำลัง update อยู่ เข้ามา refresh เรื่อยๆ นะ)
Nginx อ่านว่า เอ็นจิ้นเอ็กซ์ เรามักเห็นเว็บไซต์ดังๆ เอามาใช้เป็น Reverse Proxy ซึ่งความจริงแล้ว Nginx ก็ยังทำหน้าที่เป็น Web Server ได้ดีอีกด้วย โดยเฉพาะในการบริการ static content ทั้งหลาย เช่น รูปภาพ หรือ ไฟล์วีดีโอใหญ่ๆ
Nginx จัดว่าเป็น Web Server จำพวกเดียวกับ Lighttpd ซึ่งทำงานในแบบ non-blocking IO เหมือนกัน (ต่างจาก Apache ที่ทำงานในแบบ child process และ multi-thread) การทำงานแบบ non-blocking IO นี้ทำให้มี memory footprint น้อยมาก (และมักจะคงที่ ไม่เพิ่มขี้นจากจำนวน request ที่มากขึ้น) มีการตอบสนอง request ที่เร็ว รวมถึงรองรับปริมาณ connection ได้มากต่อช่วงเวลา ซึ่งเหมาะสำหรับการบริการ static file และการทำ reverse proxy
แม้ว่าในปัจจุบันเราจะเคยชินกับการใช้ Apache กับ module จำพวก mod_* ต่างๆ เพื่อใช้แสดงผล dynamic content เช่น mod_php, mod_perl หรือ mod_python ซึ่งเป็นการผนวกตัวแปลภาษา php, perl และ python เข้าเป็นส่วนหนึ่งของ Apache ซึ่งตามทฤษฎีแล้ว จะทำให้ความเร็วในการทำงานสูงสุด แต่เราต้องไม่ลืมว่าการทำเช่นนั้นเป็นการทำให้ memory footprint ของ Apache ใหญ่ขึ้น และหากเกิดความผิดพลาดอะไรขึ้นกับการทำงานของ mod เหล่านั้น ก็จะทำให้ Apache ทั้งก้อนตายไปด้วย
Django เมื่อใช้ร่วมกับ flup (ต้องลงในระบบโดย admin) จะสามารถ run ใน mode FastCGI ได้ โดยการใช้ FastCGI ก็คือการที่ให้ Django ทำงานอยู่ใน thread ของมันเอง เพียงแต่เปิด interface ออกมายัง port หรือทาง unix socket ที่กำหนดเองได้ ดังนั้นจึงสามารถ run Django ด้วย user account ที่ต่างจากตัว web server ได้ และ user จะสามารถ start/stop Django ได้เองตลอดเวลา (ตราบเท่าที่ user คนนั้นยังมีสิทธิ login เข้ามาในระบบ)
ดูตัวอย่างการ run Django แบบ FastCGI ได้ที่นี่
ลองเล่น Django's Comments Framework
ลองเอา Django's Comments Framework มาเล่นดู ใช้ง่ายดี แต่ว่าเหมือนจะ simple ไปหน่อย จริงๆ แล้วอาจจะมีวิธีการใช้แบบพิสดารก็ได้ แต่ยังไม่ได้ลองหาดู ตอนนี้ก็ลองมา comment กันได้
Bashell Upgraded
Ubuntu: Gutsy -> Hardy
Web Server: lighttpd -> nginx
Django: 0.97-svn -> 1.0
Django-Tag: Added to central library
from tagging import *
Most websites in bashell already down because of backward incompatible of Django-1.0. So, all bashell users must review their own code.
