You are herePlanet Django 66

Planet Django 66


Embedded Linux

Have a chance look my old hard disk and found my old document about embedded Linux. Put it here is better than left it behind.

Web API using Django and markup.py

I have a web application that has a set of web api. I want it to be XML base. Django has utilities to serialize the query set to json or XML but I want to output XML that is not django' format. Below is how I did that.

The XML generator

I use markup.py, intuitive, light weight, easy-to-use, customizable and pythonic HTML/XML generator (from its website). I didn't spend much time on this. You can use any XML generator.

Below is how I generate the XML,

Generic, URL base view count for Django

A lot of web sites have a view count statistic for showing how many views for particular web page. For Django (or any MVC web framework), we call web page base on URL. I will show you how to implement generic URL count and display it the template.

Counter models

Below is the models,

Getting to know your client using django middleware

It's important (and excite) to know how client visits your website. Especially low traffic, specific user like my blog.

You can get usage statistic by registering your site to some web statistic service . But if you want it work the way you want, you have to build it by your own!

I've built django application to get user information and store it in the database. I also have capability to log only specific path.

Another thing here, I want to band a certain set of IP address, mostly any spam that is logged by my application.

Django Hangman

At Bangkok, there are a number of open soure web framework community for example rubyonrails, seam. At these community, they have demos application powered by the framework. the demo application is Hangman.

I decided to implement Hangman in django. I have a goal to build not just a demo, it should be fun and attractive. Let get its start!

User interactive

The game will send the request using Ajax. The data send black and forth will be format in the JSON format. I will use Jquery for the javascript client library.

Settings up Mod_wsgi for Django on Webfaction

Recently, I've changed the way I deploy django application. From mod_python to mod_wsgi. I switch to mod_wsgi because of its simplicity, memory usage is smaller, setup is easy.

On Webfaction, mod_wsgi is fully supported. It is the one in the application list. Below are steps to get Django mod_wsgi up and running.

Loop in template

อยากทำ Hangman แต่ยังไม่มีเวลาว่างยาวพอที่จะเริ่ม ช่วงนี้เลยเปลี่ยนวิธีทำงาน ลด time quantum ลงด้วย rtm แทนทึ่จะเขียนทีละ 1 ชั่วโมงก็ลดให้เหลือแค่ 15 นาที หัวข้อก็จะสั้นหน่อย

วันนี้เริ่ม Hangman ซะที

python manage.py startapp hangman

<!--break-->

แล้วมาว่ากันทีละเรื่อง เริ่มจาก hangman/views.py เหมือนเดิม ไม่มีอะไรมาก แต่ส่ง list เข้ามาใน context

Static files in Django

ในตอนแรกของการสร้าง app สำหรับอัพโหลด มีขั้นตอนสำหรับการใช้ไฟล์ภายนอกไว้หน่อยนึง นี่เป็นหนึ่งในสิ่งที่จำเป็นสำหรับการทำ Hangman วันนี้ก็มาขยายความกันนิดนึง

ใน Django มีวิธีรับมือกับไฟล์ภายนอกที่อยู่นิ่งๆ ไม่เปลี่ยนแปลงอยู่ในขั้นเกือบเทพ เหมาะกับ CDN มาก ใช้ก็ง่าย แค่ระบุ url ใน settings.py

Session in Django

ก่อนที่จะเริ่มทำ Hangman ยังมีเรื่องที่ต้องรู้อีกนิดนึง อย่างแรกก็คือ Session ทำมาตั้งนานยังไม่ได้พูดถึงซักนิด ใน Django นั้น session เป็นของเสริม ไม่ใช้ก็ไม่ต้องใส่ ช่วยได้นิดหน่อย จึงต้องพูดถึงนิดนึง เริ่มจาก middleware ต้องมี django.contrib.sessions.middleware.SessionMiddleware ใน MIDDLEWARE_CLASSES