Sugree

Main menu

  • About
    • Self
  • Project
    • Barcode Generator / Reader
    • GT200
    • GeoPin
    • Mitter
    • Red Mob
    • Thairath Breaking News
    • Twitter Android SDK
    • jibjib
    • microblog-purple
    • English
    • Thai

URL routing in Django

Submitted by sugree on Mon, 09/15/2008 - 00:04
  • Django
  • Python
  • Thai

สิ่งที่ทำให้ Django ดูดีอย่างนึงก็คือ url routing ซึ่งยืดหยุ่นมากเป็นพิเศษ สาเหตุก็เพราะมันเป็น Python มันไม่ใช่โครงสร้างข้อมูลอย่าง yaml ที่มีข้อจำกัดในตัวเอง และเมื่อประกอบกับ view ที่เข้ากันอย่างดี Django ก็ดีจนต้องเลียนแบบ

คราวนี้จะลองทำให้ views.py มันดูมีประโยชน์กว่าปกติ

def entity(request, name):
    t = loader.get_template('helloworld/entity.html')
    c = Context({
        'name': name,
    })
    return HttpResponse(t.render(c))

ตามด้วย templates/helloworld/entity.html

Hello, {{ name }}!

หน้าตาประหลาด มันเริ่มมาแล้ว เดี๋ยวค่อยว่ากันวันหลัง ปิดท้ายวันนี้ด้วย urls.py

urlpatterns = patterns('',
    (r'^$', 'samplesite.helloworld.views.index'),
    (r'^(?P<name>.*)$', 'samplesite.helloworld.views.entity'),
)

url ที่ระบุสามารถกำหนด regular expression ได้ ตั้งชื่อให้เรียบร้อย แล้วมันจะถูกส่งไปให้ view ตามชื่อเอง ง่ายเชียว url พิสดารแค่ไหนก็สบาย

Roti (alpha) thinks you may like these:
  • Simple template in Django
  • MVC in Django
  • Django - Hello, World!
  • Django 66 - Django Thailand
  • Loop in template
  • Login to post comments

Popular Tags

Android BarCamp BarCampBangkok Book Django Drupal Education Family Google Hardware Hardy Health Interview Intrepid Java jibjib Joke Life microblog-purple OpenOffice.org OpenSource Podcast Python Review SoC Social Network Trip Twitter Ubuntu Weekly Suki
more tags

Powered By

debian
apache
drupal
drupal.in.th
openfreehost
inox
feedburner

© 2007-2008 Sugree Phatanapherom. Creative Commons [Feed Content / Comment]
RoopleTheme