05 Januari 2013

hello, world in django

creating project

$ django-admin startproject DOC
$ ls DOC/
__init__.py  manage.py  settings.py  urls.py
test the server
DOC$ ./manage.py runserver
get this page http://localhost:8000/
untouch project page


creating apps inside the project
$ cd DOC/
DOC$ ./manage.py startapp doc_ui
new folder doc_ui created
DOC$ ls
doc_ui  __init__.py  __init__.pyc  manage.py  settings.py  settings.pyc  urls.py

DOC$ ls doc_ui/
__init__.py  models.py  tests.py  views.py
showing a "hello, world":
  1.  make following changes 
    •  add
    (r'^doc_ui/', 'DOC.doc_ui.views.index'),
    to url_patterns in file urls.py
    • add
    'DOC.doc_ui', 
    to INSTALLED_APPS in file settings.py
  2. write this to file views.py
        from django.http import HttpResponse
        def index(request):
                return HttpResponse("Hello, world.")
run/restart the server
DOC$ ./manage.py runserver

Access through http://localhost:8000/doc_ui/ and then hello, world should show up thats all :D, but it is useless.

So far it is a beginning to write the controller part in MVC paradigm. If you have entities in your system that can be modelled as objects, django provides stunning approach to handle them as model (of MVC). The class declaration, the persistent database and the interface are seamlessly connected and provided instantly.