Sugree

Main menu

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

Loop in template

Submitted by sugree on Wed, 09/24/2008 - 11:17
  • Django
  • Python
  • Thai

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

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

python manage.py startapp hangman

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

from django.template import RequestContext
from django.shortcuts import render_to_response
 
def index(request):
    choices = [chr(65+i) for i in range(26)]
    return render_to_response('hangman/index.html',
                              {'choices': choices},
                              context_instance=RequestContext(request))

แล้วก็เซ็ต hangman/urls.py ให้รู้จัก view

from django.conf.urls.defaults import *
 
urlpatterns = patterns('',
    (r'^$', 'django66.hangman.views.index'),
)

ถัดมาก็เชื่อมกับโปรเจคใน urls.py

urlpatterns = patterns('',
    (r'^hangman/', include('django66.hangman.urls')),
)

ปิดท้ายด้วย templates/hangman/index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>   
 <title>Django HangMan</title>
</head>
<body>
{% for c in choices %}
{{ c }}
{% endfor %}
</body>
</html>

ที่อยากให้ดูก็คืออันสุดท้าย หน้าตาที่แท้จริงของ template ใส่ loop แล้วเป็นแบบนี้นี่แหละ

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

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