Python Tutorial

Saturday, November 3, 2012

django hello world


Django is python high level framework. It always encourage rapid development and clean, pragmatic design. It lets you build high-performing, elegant web applications quickly. You can also develop back-end API(django have some cool library for it). Lets go....

Create a django project.

From command prompt:
Create django project: django-admin.py startproject django_hello_world
Create django app: python manage.py startapp hello

I am using pycharm, so I can create my project from pycharm.

Here I show you only which file I need to edit for run hello world project.
You can download full projcet from here

 

// settings.py

# set admin
ADMINS = (
    ('jony', 'jony.cse@gmail.com'),
)

#set database
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', 
        # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'dev.db',                      
        # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      
         # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      
        # Set to empty string for default. Not used with sqlite3.
    }
}
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'hello', # need to install your app
)

//views.py

# create a view function which we will call
from django.http import HttpResponse
def hello_view(request):
    return HttpResponse("Hello my django!!")
	
//url.py

#call view from url
from hello import views
urlpatterns = patterns('',
    url(r'^hello_django/$', views.hello_view, name='my_hello_view'),
)




running project:

commands:
python manage.py syncbd 
# when you make chage on db, need to run this command
# create super user
python manage.py runserver 8080

Not hit this url:
http://127.0.0.1:8080/hello_django/

0 comments:

Post a Comment