2008年7月13日 星期日

class method & static method

class method:
ex:
def  test(cls):
       print('class method')
test=classmethod(test)

static method:
ex:
def  test()
      print('static method')
test=staticmethod(test)



url mapping

settings.py has a variable  ROOT_URLCONF

ROOT_URLCONF  decides the python file for routing

ROOT_URLCONF is urls.py by default

when a request comes in, Django loads ROOT_URLCONF and find the match between request url & urlpatterns 

when pattern found, the associated view function is called( the function is passed HttpRequest object as first parameter) 

the view function will return an HttpResponse object

define urlpatterns
ex:
urlpatterns = patterns( '', ( r'^test/$',  testFunction) )
match  /test/ ,  if request is /test/,  testFunction is called

define view function:
ex:
def test(request):
now=datetime.datetime.now()
html= "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)

raw string

using r:
turn off escpate
ex:
tempStr=  r'C:\test'

python path

print python path:
import sys
print  sys.path

create a project, create an app, run the server, path, timezone

create a projct
django-admin.py  startproject  newSite

create an app in the project:
python manage.py   startapp books
create  __init__.py, models.py,  views.py in books directory 

run the server:
python manage.py   runserver
running at 127.0.0.0.1:8000

python manage.py  runserver  0.0.0.0  8888
listen on any network interface,  on port 8888

set path for django project
add django_site.pth  under /Library/Python/2.5/site-packages
ex:
django_site.pth:
/Volumes/Data/django_project/

time zone
in settings.py
TIME_ZONE = 'Asia/Taipei'

install django

sudo python setup.py install

__name__ & __main__

when the file is run as a top-level program,  __name__ is set as __main__

when the file is imported,  __name__ is set to the module's name

Hence, we can use __name__ to do unit test.
If  __name__ == __main__,   we execute unit test
if __name__ != __main__,  unit test is not executed