Monday, December 16, 2013

Paint app using Django and sqlite3 as databae

Django is a web framework designed to help us build complex web applications simply and quickly. It’s written in the Python programming language.Django takes it name from the early jazz guitarist Django Reinhardt, a gypsy savant who managed to play dazzling and electrifying runs on his instrument even though two of the fingers on his left hand were paralyzed in an accident when he was young.Thus, it’s a fitting name for the framework: Django can do some very complex things with less code and a simpler execution than you’d expect. It doesn’t take a heavy hand to build with Django. The framework does the repetitive work for you, allowing you to get a working website up quickly and easily.

Django setup:

After install django,make a directory to store our project.

Open the terminal and enter the following commands:

mkdir djando
cd django
django-admin.py startproject mysite

this produces:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

This is the general structure of django.Here we didnt add any apps.

First we have to setup the database.Open settings.py and do as following.Here i am using sqlite3 database.


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test.db',
    }
}

Then enter the command:

python manage.py syncdb   #Creates database and corresponding tables for django

Now its time to add our paint application.so

python manage.py startapp paint

this produces following directory like

├── manage.py
├── paint
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Here we are...

Here model view controller(MVC) is the routing procedure or model template view.
In models.py we have to setup database model
In views.py we have to setup routing functions or rendering functions
In urls.py corresponding urls are determined

Then we have to add this app in settings.py

INSTALLED_APPS = (
#    'django.contrib.admin',
#    'django.contrib.auth',
#    'django.contrib.contenttypes',
#    'django.contrib.sessions',
#   'django.contrib.messages',
    'django.contrib.staticfiles',
    'paint',
)


In models.py

from django.db import models

class Pic(models.Model):
        name=models.CharField(max_length=100)
        data=models.CharField(max_length=10000000000)

Then we have to create template directory and static directory inside.
So this will like

├── manage.py
├── paint
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── templates
└── static

Then htmls routing functions are done.To view the codes click here

To view the site i deployed on heroku.Click here

No comments:

Post a Comment