Sugree

Main menu

  • About
    • Self
  • Project
    • GT200
    • GeoPin
    • Mitter
    • Red Mob
    • Thairath Breaking News
    • jibjib
    • microblog-purple
    • English
    • Thai

Simple template in Django

Submitted by sugree on Fri, 09/12/2008 - 17:42
  • Django
  • Python
  • Thai

ลองมาดู template ใน Django กันบ้าง หลักการง่ายๆ ไม่ใช้ xml ไม่ใช่ Python ต้องง่ายกว่านั้น ทำงานได้สารพัด ไม่จำกัดรูปแบบ ขั้นแรกต้องบอกก่อนว่า template อยู่ที่ไหน ข้อมูลพวกนี้อยู่ใน settings.py เลือกใช้ได้ตามสภาพแวดล้อม

TEMPLATE_DIRS = (
    '/home/sugree/work/git/django66/samplesite/templates'
)

อย่าลืมสร้างไดเรกทอรีด้วย

mkdir templates

หน้าตา views.py จะเปลี่ยนไป

from django.template import Context, loader
from django.http import HttpResponse
 
def index(request):
    t = loader.get_template('helloworld/index.html')
    c = Context()
    return HttpResponse(t.render(c))

ตัดค่าคงที่ออกไป แล้วเอาไปใส่ใน templates/helloworld/index.html

Hello, World!

คำเตือน: ขอเปลี่ยนชื่อ view เป็น index เลยต้องแก้ urls.py ตามไปด้วย

urlpatterns = patterns('',
    (r'^$', 'samplesite.helloworld.views.index'),
)
Roti (alpha) thinks you may like these:
  • Simple template in Django
  • MVC in Django
  • Django - Hello, World!
  • Django 66 - Django Thailand
  • Loop in template
by chanwit (not verified) on Sun, 09/14/2008 - 00:17 #504

อ่านตามเรื่อย ๆ เริ่มจะเห็นภาพ

context คงเป็นตัวเอาไว้ hold ค่าที่ retrieve จาก model ?

กลไกการ resolve view มัน hard code ไปนิด คงเป็นเหตุผลเรื่อง simplicity ผมสนใจว่ามี view แบบอื่นอีกมั้ยครับนอกจากแบบนี้

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
Input format
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockcode> <pre>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <html>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <html>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use Markdown syntax to format and style the text. Also see and Markdown Extra for tables, footnotes, and more.
  • Twitter-style @usersnames are linked to their Twitter account pages.
  • Twitter-style #hashtags are linked to search.twitter.com.

More information about formatting options

Popular Tags

Android BarCamp BarCampBangkok BarCampBangkok2 Book Django Drupal Education Family Google Hardware Hardy Health Interview 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